简体   繁体   English

Android共享图片不起作用

[英]Android sharing image doesn't work

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: 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)

The problem: When I try to share with gmail, gmail is force closed. 问题:当我尝试与gmail共享时,gmail被强制关闭。 When I try to share with Facebook, Facebook silently rejects the post. 当我尝试与Facebook分享时,Facebook默默拒绝该帖子。 Messaging brings up the messenger, but is empty. 消息传递信使,但是空洞。 Sharing works without adding in the image. 共享无需添加图像即可工作。

First, never use concatenation to build file paths, let alone Uri values. 首先,永远不要使用连接来构建文件路径,更不用说Uri值了。

Second, EXTRA_STREAM is supposed to hold a Uri , not a String . 其次, EXTRA_STREAM应该包含一个Uri ,而不是一个String

Third, since you know the right MIME type ( image/png ), use it, instead of a wildcard. 第三,既然您知道正确的MIME类型( image/png ),请使用它而不是通配符。

Fourth, never build the same path twice. 第四,永远不要两次建立相同的路径。 Here you create File image the right way, then ignore that value. 在这里,您以正确的方式创建File image ,然后忽略该值。

So, dump the String url line, replace image/* with image/png , and modify: 因此,转储String url行,将image/*替换为image/png ,然后修改:

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

to be: 成为:

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

Also, consider using the android.support.v4.content.FileProvider class to share your file using a content URI instead of a file URI. 另外,请考虑使用android.support.v4.content.FileProvider类来使用内容URI而不是文件URI来共享文件。 It's more secure. 它更安全。 See the reference documentation for FileProvider 请参阅FileProvider参考文档

You need to pass Content URI all the time (at least in Android 5.1+). 您需要始终传递内容URI(至少在Android 5.1+中)。 here's how to get a content path from a Bitmap : 这是如何从位图获取内容路径:

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

And then to 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..."));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM