Issue
I'm having a share button which would share an image from drawable with text.
public boolean shareGame(String msg) {
if (Configuration.Share_WITH_IMAGE) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/png");
Uri uri = Uri
.parse("android.resource://" + getPackageName() + "/" + R.drawable.share_image);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(Intent.EXTRA_TEXT, msg + " " + GOOGLE_PLAY_URL);
startActivity(Intent.createChooser(shareIntent, "Share: " + msg));
} else {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, msg + GOOGLE_PLAY_URL);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, msg));
}
return true;
}
Currently, it returns an empty file without any extension when I try to share it through email. WhatsApp share gives an error of unsupported file type.
Do I need to convert this to Bitmap? If so, what's the best way to do it.
Thanks.
Solution
this code works.. i have tested it for both gmail and whatsapp
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "share_image.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_TEXT, "send text");
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file:///sdcard/share_image.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
Edit: i dont know why you are getting that error hope you have added permissions if not add following permissions
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Answered By - user1950395
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.