I am trying to attach image from my gridview to gmail or facebook,but whenever i tried to attach my app got crash,and i am getting following error with nullpointer exceptio
This is what worked for me perfectly.
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_subject));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, getResources().getString(R.string.share_message));
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.image_to_share);
startActivity(Intent.createChooser(shareIntent, "Share Image"));
But I will always recommend to decode a Bitmap
from this resource, save it as a File
in the storage and then use that file. A more reliable way this directly using from the resources.
Here is the working code which you need:
Firstly save image from Drawable
to SD Card
here is the code:
try{
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.bubble_green);
//replace "R.drawable.bubble_green" with the image resource you want to share from drawable
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
largeIcon.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
// you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg");
f.createNewFile();
// write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Then get the saved image from SD card
and attach in the email intent like this:
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hi"); //set your subject
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "How are you"); //set your message
String imagePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share Image"));
First Convert your drwable to image and store it on SDCard
Store Drawable to SD card
Then
private void shareImage() {
Intent share = new Intent(Intent.ACTION_SEND);
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");
// Make sure you put example png image named yourImg.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
+ "/yourImg.png";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}
roman thing is you are passing a null bitmap object
Bitmap icon = mBitmap;
you need to assign bitmap first then the code will start work
for more info you can have a look on to below links :-
https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents
http://developer.android.com/training/sharing/send.html