Reputation: 3
i am unable to pass the image using share intent.The app runs but when i try to send image to a friend in whatsApp it says "Sharing failed, please try again" and on sharing with gmail it says "Cannot attach empty file" .I have also set permission in Manifests. I guess there is some issue in creating file or maybe the path.
please Check the below code:
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap icon = BitmapFactory.decodeResource(getResources(), img);
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sharingIntent.setType("image/*");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator + "temporary_file.jpg");
// File f1=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
//File f = new File(Environment.getExternalStorageDirectory() + "/save_picture", "temporary_file.jpg");
try {
f.createNewFile();
// f.mkdir();
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
// sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + Environment.getExternalStorageDirectory()));
sharingIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("content:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(sharingIntent, "Share via: "));
}
});
Upvotes: 0
Views: 742
Reputation: 1667
I think the URI is malformed. Try this:
Uri.fromFile(file);
instead of
Uri.parse("content:///sdcard/temporary_file.jpg"); // Here probably file path is wrong
Using Uri.fromFile
you ensures that the file URI is correct.
If your targetSdkVersion is 24+ you need to use content:// instead of file://. To migrate, checkout this question.
Upvotes: 1
Reputation: 1992
For android 6 and higher, adding permissions in manifest is not enough, you have to request for permission at the runtime. For example, for external storage permission, you have to do this :
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
// permission granted, you can write external storage
}
If is not granted, you have to request the permission :
final int READ_STORAGE_PERMISSION_REQUEST_CODE = 1212;
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
WRITE_EXTERNAL_STORAGE);
}
For reading external storage, you have to remplace WRITE_EXTERNAL_STORAGE
by READ_EXTERNAL_STORAGE
EDIT :
Override onRequestPermissionsResult in your activity :
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] results) {
super.onRequestPermissionsResult(requestCode, permissions, results);
if(PackageManager.PERMISSION_GRANTED == grantResults[0]){
//The user accepted the permissions requests, you can do your stuffs here
}
}
You can find more informations here
Upvotes: 0