图片分享的意图适用于Gmail的崩溃FB和Twitter [英] Image share intent works for Gmail but crashes FB and twitter

查看:244
本文介绍了图片分享的意图适用于Gmail的崩溃FB和Twitter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许用户共享图像的设备上的其他应用程序。该图像是我的应用程序的内部存储区域的文件/子目录里面。它工作得很好与Gmail,但Facebook和Twitter这两个崩溃时回应我的意图。

I am trying to allow the user to share an image to other apps on the device. The image is inside the files/ subdirectory of my app's internal storage area. It works just fine with Gmail, but Facebook and Twitter both crash when responding to my intent.

编辑:Google+的也能正常工作

Google+ also works fine.

下面是code中的相关部分。

Here are the relevant sections of code.

在的Application.xml

In Application.xml

<provider 
    android:name="android.support.v4.content.FileProvider"
    android:authorities="org.iforce2d.myapp.MyActivity"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepaths" />
</provider>

XML / filepaths.xml

xml/filepaths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="shared" path="shared"/>
</paths>

下面是共享code。在我的活动:

Here is the sharing code in my activity:

File imagePath = new File(getContext().getFilesDir(), "shared");
File newFile = new File(imagePath, "snapshot.jpg");
Uri contentUri = FileProvider.getUriForFile(getContext(),
                     "org.iforce2d.myapp.MyActivity", newFile);    

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

List<ResolveInfo> resInfos = 
    getPackageManager().queryIntentActivities(shareIntent,
                                              PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo info : resInfos) {
    getContext().grantUriPermission(info.activityInfo.packageName, 
                                    contentUri,
                                    Intent.FLAG_GRANT_READ_URI_PERMISSION);
}

startActivity(Intent.createChooser(shareIntent, "Share image..."));

contentUri 的记录时的值是:

内容://org.iforce2d.myapp.MyActivity/shared/snapshot.jpg

我只查这与Gmail,Facebook和Twitter作为接收的应用程序,但结果已在广泛的OS版本(从2.2.1至4.4.3)和7器件包含一个点燃的非常一致。

I have only checked this with Gmail, Facebook and Twitter as the receiving apps, but the results have been very consistent over a wide range of OS versions (from 2.2.1 to 4.4.3) and 7 devices include a Kindle.

Gmail的伟大工程。图片缩略图显示在邮件编写,并成功地连接到邮件发送时。

Gmail works great. Image thumbnail appears in mail composition, and successfully attaches to mail when sent.

Twitter和Facebook这两个碰撞,如下图所示。

Twitter and Facebook both crash, as below.

下面是堆栈跟踪从logcat中显示这两个应用程序是有问题的,这似乎是对他们俩的同样的问题(这是从4.4.3,但错误是几乎相同的所有回来的路上2.2.1虽然略有不同的写法错误消息):

Here are the stack traces from logcat showing the problem these two apps are having, it appears to be the same problem for both of them (this is taken from 4.4.3, but the errors were practically the same all the way back to 2.2.1 albeit with slightly different wording of the error message):

Caused by: 
  java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. 
  Make sure the Cursor is initialized correctly before accessing data from it.
    at android.database.CursorWindow.nativeGetString(Native Method)
    at android.database.CursorWindow.getString(CursorWindow.java:434)
    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
    at android.database.CursorWrapper.getString(CursorWrapper.java:114)
    at com.twitter.library.media.util.f.a(Twttr:95)
    ...

Caused by: 
  java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.
  Make sure the Cursor is initialized correctly before accessing data from it.
    at android.database.CursorWindow.nativeGetString(Native Method)
    at android.database.CursorWindow.getString(CursorWindow.java:434)
    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
    at android.database.CursorWrapper.getString(CursorWrapper.java:114)
    at com.facebook.photos.base.media.MediaItemFactory.b(MediaItemFactory.java:233)
    ...

由于Facebook和Twitter上分享照片是数以百万计的人们整天,我是pretty的震撼,这是很难实现的东西:/

Given that sharing images on Facebook and Twitter is something that millions of people do all day long, I'm pretty shocked that it's so hard to implement :/

任何人都可以点什么我做错了什么?

Can anybody spot something I'm doing wrong here?

推荐答案

编辑 - 在这个答案的作品中描述的解决办法,但的斯特凡提供的解决方案更优雅。它应该是公认的答案。

EDIT -- The workaround described in this answer works, but the solution provided by Stefan is more elegant. It should be the accepted answer.

此外,截至2015年1月,Facebook应用程序不再有这个bug(和现在的作品与标准 FileProvider ),但Twitter仍然如此。希望这会彻底消失在未来,这些都不是必要的。

Also, as of January 2015, the Facebook app no longer has this bug (and now works with the standard FileProvider) but Twitter still does. Hopefully it will go completely away in the future, and neither of these will be necessary.

虽然 FileProvider 物体看起来精彩在理论上,它似乎并没有分享到许多第三方应用程序时(工作,而谷歌的,如G +,Gmail的,和C完美地工作)。要么它不公开足够的信息,或者这些应用程序只是假设您共享文件,因此崩溃。

While the FileProvider object looks wonderful in theory, it doesn't seem to work when sharing to many third-party applications (while Google ones, like G+, Gmail, &c work perfectly). Either it doesn't expose sufficient information, or those applications just assume you're sharing files, and thus crash.

这是很无奈! :/

我发现共享图像文件的唯一可靠的方法是将其复制到外部存储目录(你可以创建一个 .nomedia 子目录,以避免他们出现在画廊的应用程序)。

The only reliable way I've found to share image files is to copy them to the external storage directory (you can create a .nomedia subdirectory to avoid them appearing in the Gallery app).

例如:

Bitmap bitmapToShare = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

File pictureStorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File noMedia = new File(pictureStorage, ".nomedia");
if (!noMedia.exists())
    noMedia.mkdirs();

File file = new File(noMedia, "shared_image.png");
saveBitmapAsFile(bitmapToShare, file);

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
shareIntent.setType("image/png");

startActivity(shareIntent);

附录:当然,你也应该检查 getExternalStorageState()之前试图写入/复制文件有

Addendum: of course, you should also check getExternalStorageState() before trying to write/copy the file there.

这篇关于图片分享的意图适用于Gmail的崩溃FB和Twitter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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