2017-12-24 14 views
0

"共有"ボタンを実装しようとしています。写真を送る必要があります。画像を別のアプリケーションに送信

私がやっているものです:

Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); 

File outputDir = context.getCacheDir(); 
File outputFile = null; 
try { 
    outputFile = File.createTempFile("temp_", ".jpg", outputDir); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

FileOutputStream fileOutputStream = null; 
try { 
    fileOutputStream = new FileOutputStream(outputFile); 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} 

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream); 

try { 
    fileOutputStream.close(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

Intent shareIntent = new Intent(); 
shareIntent.setAction(Intent.ACTION_SEND); 
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(outputFile)); 
shareIntent.setType("image/jpeg"); 
startActivity(Intent.createChooser(shareIntent, 
getResources().getText(R.string.send_via))); 

をしかし、私はそれが画像をアップロードすることは不可能だというメッセージを取得します。どうしたの?

答えて

0

第三者のアプリケーションでは、internal storage部分のファイルにアクセスする権利がありません。

第2に、Android 7.0以降では、Uri.fromFile()によって返された値など、fileUriの値を使用することはできません。

両方の問題を解決するには、FileProviderを使用して、他のアプリから画像を利用できるようにします。 Uri.fromFile()の代わりにFileProvider.getUriForFile()を使用し、IntentにはFLAG_GRANT_READ_URI_PERMISSIONを必ず追加してください。

This sample appは、サードパーティのアプリとFileProviderを使用して実証(ACTION_IMAGE_CAPTUREACTION_VIEWのために、同じ技術がACTION_SENDのために動作します)。

関連する問題