Android sharing image not working

I am trying to share a screenshot of the application using the following code:

View content = findViewById(R.id.layoutHome); content.setDrawingCacheEnabled(true); Bitmap bitmap = content.getDrawingCache(); File sdCardDirectory = Environment.getExternalStorageDirectory(); File image = new File(sdCardDirectory,"temp.png"); // Encode the file as a PNG image. FileOutputStream outStream; try { outStream = new FileOutputStream(image); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); outStream.flush(); outStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String url = "file://" + sdCardDirectory.toString() + "Images/temp.png"; Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); sharingIntent.setType("image/*"); String shareBody = "Here is the share content body"; sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject Here"); sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, url); sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,shareBody); startActivity(Intent.createChooser(sharingIntent, "Share via")); 

Logcat:

 10-10 14:20:16.631: W/Bundle(16349): Key android.intent.extra.STREAM expected Parcelable but value was a java.lang.String. The default value <null> was returned. 10-10 14:20:16.658: W/Bundle(16349): Attempt to cast generated internal exception: 10-10 14:20:16.658: W/Bundle(16349): java.lang.ClassCastException: java.lang.String cannot be cast to android.os.Parcelable 10-10 14:20:16.658: W/Bundle(16349): at android.os.Bundle.getParcelable(Bundle.java:1171) 10-10 14:20:16.658: W/Bundle(16349): at android.content.Intent.getParcelableExtra(Intent.java:4140) 10-10 14:20:16.658: W/Bundle(16349): at android.content.Intent.migrateExtraStreamToClipData(Intent.java:6665) 10-10 14:20:16.658: W/Bundle(16349): at android.content.Intent.migrateExtraStreamToClipData(Intent.java:6650) 10-10 14:20:16.658: W/Bundle(16349): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1410) 10-10 14:20:16.658: W/Bundle(16349): at android.app.Activity.startActivityForResult(Activity.java:3351) 10-10 14:20:16.658: W/Bundle(16349): at android.app.Activity.startActivityForResult(Activity.java:3312) 10-10 14:20:16.658: W/Bundle(16349): at android.app.Activity.startActivity(Activity.java:3522) 10-10 14:20:16.658: W/Bundle(16349): at android.app.Activity.startActivity(Activity.java:3490) 10-10 14:20:16.658: W/Bundle(16349): at com.example.simplegraph.EconActivity$DrawerItemClickListener.onItemClick(EconActivity.java:182) 10-10 14:20:16.658: W/Bundle(16349): at android.widget.AdapterView.performItemClick(AdapterView.java:298) 10-10 14:20:16.658: W/Bundle(16349): at android.widget.AbsListView.performItemClick(AbsListView.java:1086) 10-10 14:20:16.658: W/Bundle(16349): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855) 10-10 14:20:16.658: W/Bundle(16349): at android.widget.AbsListView$1.run(AbsListView.java:3529) 10-10 14:20:16.658: W/Bundle(16349): at android.os.Handler.handleCallback(Handler.java:615) 10-10 14:20:16.658: W/Bundle(16349): at android.os.Handler.dispatchMessage(Handler.java:92) 10-10 14:20:16.658: W/Bundle(16349): at android.os.Looper.loop(Looper.java:137) 10-10 14:20:16.658: W/Bundle(16349): at android.app.ActivityThread.main(ActivityThread.java:4745) 10-10 14:20:16.658: W/Bundle(16349): at java.lang.reflect.Method.invokeNative(Native Method) 10-10 14:20:16.658: W/Bundle(16349): at java.lang.reflect.Method.invoke(Method.java:511) 10-10 14:20:16.658: W/Bundle(16349): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 10-10 14:20:16.658: W/Bundle(16349): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-10 14:20:16.658: W/Bundle(16349): at dalvik.system.NativeStart.main(Native Method) 

Problem: When I try to use gmail, gmail is forced to close. When I try to share with Facebook, Facebook silently rejects the message. Messages bring a message, but empty. Sharing works without adding an image.

+7
android android-intent android-sdcard android-sharing
source share
3 answers

First, never use concatenation to create file paths, let alone Uri values.

Secondly, EXTRA_STREAM should contain a Uri , not a String .

Third, since you know the correct MIME type ( image/png ), use it instead of a wildcard.

Fourth, never create the same path twice. Here you create the File image correct path, then ignore this value.

So, unload the String url string, replace image/* with image/png and change:

 sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, url); 

:

 sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(file)); 
+17
source share

Also, consider using the android.support.v4.content.FileProvider class to share your file using the content URI, not the file URI. It is safer. See Help documentation for FileProvider

+1
source share

You need to constantly skip the Content URI (at least in Android 5.1+). here's how to get the content path from a bitmap:

 Bitmap bitmap;//this should be your bitmap String MediaFilePath = Images.Media.insertImage(MainActivity.getContentResolver(), bitmap, FileName, null); 

And then share:

 public static void ShareFile(String ContentPath, String Mime) { Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); sharingIntent.setType(Mime); Uri FileUri = Uri.parse( ContentPath ); sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); sharingIntent.putExtra(Intent.EXTRA_STREAM, FileUri); MainActivity.startActivity(Intent.createChooser(sharingIntent, "Share to...")); } 
+1
source share

All Articles