Android的共享图像不工作 [英] Android sharing image doesn't work

查看:169
本文介绍了Android的共享图像不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用下面的code共享的应用程序的截图:

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)

的问题: 当我尝试使用Gmail分享,Gmail是强制关闭。当我尝试与Facebook分享,Facebook的默默拒绝的职位。消息带来了使者,但是是空的。分享作品,未经图像中添加。

The problem: When I try to share with gmail, gmail is force closed. When I try to share with Facebook, Facebook silently rejects the post. 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.

其次, EXTRA_STREAM 应该持有乌里,不是字符串

Second, EXTRA_STREAM is supposed to hold a Uri, not a String.

第三,既然你知道正确的MIME类型(图像/ PNG ),而不是使用通配符它。

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

四,永远无法相同的路径两次。在这里,您将创建文件图像以正确的方式,则忽略该值。

Fourth, never build the same path twice. Here you create File image the right way, then ignore that value.

因此​​,转储字符串URL 行,替换图像/ * 图像/ PNG ,并修改

So, dump the String url line, replace image/* with image/png, and modify:

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

是:

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

这篇关于Android的共享图像不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆