2017-06-20 3 views
0

私は誰もが問題があります。私は自分のアプリケーションからwhatsappに画像を送る必要があります。ビットマップ形式で画像をダウンロードしました。 jpgでビットマップ形式を変換した後、画像を送信しようとしました。しかし、whatsappはデータを受け取っていません。イメージをwhatsappに送信する

Intent shareIntent = new Intent(); 
shareIntent.setAction(Intent.ACTION_SEND); 
shareIntent.setType("image/jpeg"); 

Bitmap bmp = getBitmap(); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream); 

File f = new File(getAppContext().getCacheDir() + File.separator + "temporary_file.jpg"); 
try { 
     f.createNewFile(); 
     FileOutputStream fo = new FileOutputStream(f); 
     fo.write(stream.toByteArray()); 
     fo.flush(); 
     fo.close(); 
} catch (IOException e) { 
     e.printStackTrace(); 
} 

shareIntent.putExtra(Intent.EXTRA_STREAM, f.getPath()); 
myShareActionProvider.setShareIntent(shareIntent); 

私はマニフェストにすべての権限を挿入しています。 どうすれば修正できますか?おかげさまで

PS。私はmin sdk 17とmax sdkを使用します。

+0

はhttps://stackoverflow.com/a/44639312/115145 – CommonsWare

答えて

1

WhatsAppはgetCacheDir()によって返されたディレクトリにアクセスすることはできません。代わりに、このようにそれをやってみてください。これが機能する

File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg"); 

、あなたのManifest.xmlファイル内WRITE_EXTERNAL_STORAGEパーミッションを宣言する必要があります。このように:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

私はEnvironment.getExtrnalStorageDirectory()を使用してファイルを作成しようとしているが、私はでCreateNewFile()しようとすると、それはIOExceptionが権限を返すには、拒否されました – apollo9

+0

manifest.xmlファイルにWRITE_EXTERNAL_STORAGE権限を追加する必要があるため、回答を編集します。 – Tharkius

関連する問題