2016-04-06 21 views
-1

明示的な意図で画面のスクリーンショットを渡そうとしていますが、画面に黒いスクリーンショットが表示されます(画像here参照)。共有をクリックするとすぐに、送信失敗を示すトーストが表示されます。ここでスクリーンショットをキャプチャし、他のアプリにそれを送信するためのコードがあります:インテントとして渡されたときに画像が表示されない

public void getScreenShot(View view) { 
     View screenView = view.getRootView(); 
     screenView.setDrawingCacheEnabled(true); 
     Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache()); 
     screenView.setDrawingCacheEnabled(false); 
     f = new File(this.getFilesDir(), "screenshotFile"); 
     try { 
      if (!f.exists()) 
       f.createNewFile(); 
     } catch (IOexception e) { 
      e.printStackTrace(); 
     } 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 10, bos); 
     byte[] bitmapdata = bos.toByteArray(); 

     FileOutputStream fos = null; 
     try { 
      fos = new FileOutputStream(f); 
      fos.write(bitmapdata); 
      fos.flush(); 
      fos.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

このコードはのWhatsAppするデータを送信します。

public void shareWhatsapp(View view) { 
    try { 
     myVib.vibrate(50); 
     getScreenShot(view); 

     //String fileName = "screenshotFile"; 
     //Bitmap bitmap = BitmapFactory.decodeFile(f.getAbsolutePath()); 

     Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.setType("image/*"); 
     try { 
      intent.setPackage("com.whatsapp"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Toast.makeText(MainActivity.this, "App not installed", Toast.LENGTH_SHORT).show(); 
     } 
     //TODO: APP CAN CRASH HERE 
     if (position > 0) { 
      try { 
       intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } finally { 
       intent.putExtra(Intent.EXTRA_TEXT, Titles.get(position - 1) + ": " + Links.get(position - 1));  //position problems 
      } 
     } else { 
      try { 
       intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } finally { 
       intent.putExtra(Intent.EXTRA_TEXT, Titles.get(0) + ": " + Links.get(0));  //position problems 
      } 
     } 
     startActivity(intent); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

誰かがこれで私を助けることができますか?

+0

Androidのどのバージョンでこれをテストしていますか? –

+0

Android 6.0 MarshMallow – GurpreetSK95

+0

多分問題を全然読まないほどの自由時間があるかもしれないので、ダウンボッターが助けてくれるかもしれません。 – GurpreetSK95

答えて

0

まず、ファイルをinternal storageに書き込みます。サードパーティのアプリは、アプリの内部ストレージにアクセスできない

第2に、、つまりstarting to be discontinuedを使用しています。

最も安全な長期的な行動は、have a ContentProvider serve your file from its location on internal storageです。次に、ContentProviderに関連付けられたUriを使用してください。

+0

外部ストレージへの保存を試みても、問題は解決されません。カントがファイルにアクセスします。マニフェストに記載されているすべての権限。 java.io.filenotfoundeceptionオープンが失敗しました(許可が拒否されました) – GurpreetSK95

関連する問題