2017-12-24 6 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 부분의 파일에 액세스 할 수있는 권한이 없습니다.

둘째, Android 7.0 이상에서는 Uri.fromFile()에 의해 반환되는 값과 같이 fileUri 값을 사용할 수 없습니다.

두 가지 문제를 모두 해결하려면 FileProvider을 사용하여 다른 응용 프로그램에서 이미지를 사용할 수있게하십시오. Uri.fromFile() 대신 FileProvider.getUriForFile()을 사용하고 IntentFLAG_GRANT_READ_URI_PERMISSION을 추가하십시오.

This sample app 타사 응용 프로그램과 FileProvider을 사용하는 방법을 보여줍니다 (ACTION_IMAGE_CAPTUREACTION_VIEW을 위해,하지만 같은 기술은 ACTION_SEND을 위해 작동합니다).