Android共享图片不起作用

前端之家收集整理的这篇文章主要介绍了Android共享图片不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用以下代码共享应用程序的屏幕截图:
  1. View content = findViewById(R.id.layoutHome);
  2. content.setDrawingCacheEnabled(true);
  3. Bitmap bitmap = content.getDrawingCache();
  4.  
  5. File sdCardDirectory = Environment.getExternalStorageDirectory();
  6. File image = new File(sdCardDirectory,"temp.png");
  7.  
  8. // Encode the file as a PNG image.
  9. FileOutputStream outStream;
  10. try {
  11. outStream = new FileOutputStream(image);
  12. bitmap.compress(Bitmap.CompressFormat.PNG,100,outStream);
  13. outStream.flush();
  14. outStream.close();
  15. } catch (FileNotFoundException e) {
  16. e.printStackTrace();
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }
  20.  
  21. String url = "file://" + sdCardDirectory.toString() + "Images/temp.png";
  22.  
  23. Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
  24. sharingIntent.setType("image/*");
  25. String shareBody = "Here is the share content body";
  26. sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject Here");
  27. sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,url);
  28. sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,shareBody);
  29. startActivity(Intent.createChooser(sharingIntent,"Share via"));

logcat的:

  1. 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.
  2. 10-10 14:20:16.658: W/Bundle(16349): Attempt to cast generated internal exception:
  3. 10-10 14:20:16.658: W/Bundle(16349): java.lang.ClassCastException: java.lang.String cannot be cast to android.os.Parcelable
  4. 10-10 14:20:16.658: W/Bundle(16349): at android.os.Bundle.getParcelable(Bundle.java:1171)
  5. 10-10 14:20:16.658: W/Bundle(16349): at android.content.Intent.getParcelableExtra(Intent.java:4140)
  6. 10-10 14:20:16.658: W/Bundle(16349): at android.content.Intent.migrateExtraStreamToClipData(Intent.java:6665)
  7. 10-10 14:20:16.658: W/Bundle(16349): at android.content.Intent.migrateExtraStreamToClipData(Intent.java:6650)
  8. 10-10 14:20:16.658: W/Bundle(16349): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1410)
  9. 10-10 14:20:16.658: W/Bundle(16349): at android.app.Activity.startActivityForResult(Activity.java:3351)
  10. 10-10 14:20:16.658: W/Bundle(16349): at android.app.Activity.startActivityForResult(Activity.java:3312)
  11. 10-10 14:20:16.658: W/Bundle(16349): at android.app.Activity.startActivity(Activity.java:3522)
  12. 10-10 14:20:16.658: W/Bundle(16349): at android.app.Activity.startActivity(Activity.java:3490)
  13. 10-10 14:20:16.658: W/Bundle(16349): at com.example.simplegraph.EconActivity$DrawerItemClickListener.onItemClick(EconActivity.java:182)
  14. 10-10 14:20:16.658: W/Bundle(16349): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
  15. 10-10 14:20:16.658: W/Bundle(16349): at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
  16. 10-10 14:20:16.658: W/Bundle(16349): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
  17. 10-10 14:20:16.658: W/Bundle(16349): at android.widget.AbsListView$1.run(AbsListView.java:3529)
  18. 10-10 14:20:16.658: W/Bundle(16349): at android.os.Handler.handleCallback(Handler.java:615)
  19. 10-10 14:20:16.658: W/Bundle(16349): at android.os.Handler.dispatchMessage(Handler.java:92)
  20. 10-10 14:20:16.658: W/Bundle(16349): at android.os.Looper.loop(Looper.java:137)
  21. 10-10 14:20:16.658: W/Bundle(16349): at android.app.ActivityThread.main(ActivityThread.java:4745)
  22. 10-10 14:20:16.658: W/Bundle(16349): at java.lang.reflect.Method.invokeNative(Native Method)
  23. 10-10 14:20:16.658: W/Bundle(16349): at java.lang.reflect.Method.invoke(Method.java:511)
  24. 10-10 14:20:16.658: W/Bundle(16349): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  25. 10-10 14:20:16.658: W/Bundle(16349): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  26. 10-10 14:20:16.658: W/Bundle(16349): at dalvik.system.NativeStart.main(Native Method)

问题:
当我尝试与gmail共享时,gmail被强制关闭.当我尝试与Facebook分享时,Facebook默默地拒绝该帖子.消息传递信使,但是空的.共享无需添加图像即可工作.

解决方法

首先,永远不要使用连接来构建文件路径,更不用说Uri值了.

其次,EXTRA_STREAM应该包含一个Uri,而不是一个String.

第三,既然您知道正确的MIME类型(image / png),请使用它而不是通配符.

第四,永远不要两次建立相同的路径.在这里,您以正确的方式创建文件图像,然后忽略该值.

因此,转储String url行,将image / *替换为image / png,然后修改

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

成为:

  1. sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,Uri.fromFile(file));

猜你在找的Android相关文章