无法使用Gmail共享资源中的图片

时间:2021-01-20 20:41:49

I try to share an image from assets. Gmail shows a toast "Can't attach empty file". When I try another email client the app shows me the attachment preview and when I press send button the email app crashes or closes I am not sure. The sharing works well with skype. I also tried to share the image with VK. It loads and shows me the thumbnail successfully, but after loading it shows Error label on the image. I don't know how it works, cause I don't provide any thumbnails.

我尝试从资产中分享图像。 Gmail显示敬酒“无法附加空文件”。当我尝试另一个电子邮件客户端时,该应用程序会向我显示附件预览,当我按下发送按钮时,电子邮件应用程序崩溃或关闭我不确定。共享适用于Skype。我还尝试与VK分享图像。它加载并成功显示缩略图,但加载后它会在图像上显示错误标签。我不知道它是如何工作的,因为我没有提供任何缩略图。

This is my ContentProvider:

这是我的ContentProvider:

public class AssetsContentProvider extends ContentProvider {
    public static final String TAG = AssetsContentProvider.class.getSimpleName();

    @Override
    public boolean onCreate() {
        return true;
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
                        String sortOrder) {
        return null;
    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
        Context context = getContext();
        if (context == null) {
            throw new RuntimeException("content == null");
        }

        AssetManager assetManager = context.getAssets();
        String filePath = uri.getPath();

        if (filePath != null) {
            filePath = Strings.removeFromStart(filePath, "/");
        }
        if(Strings.isEmpty(filePath)) {
            Log.e(TAG, "filePath is empty, invalid uri");
            throw new FileNotFoundException("filePath is empty, invalid uri");
        }

        AssetFileDescriptor fileDescriptor = null;
        try {
            fileDescriptor = assetManager.openFd(filePath);
        } catch (IOException e) {
            Log.e(TAG, "assetManager.openFd failed", e);
        }
        return fileDescriptor;
    }
}

assetManager.openFd(filePath); runs successfully.

assetManager.openFd(文件路径);运行成功。

This is how I share the image:

这是我分享图像的方式:

public static void shareImage(Context context, Uri uri) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    context.startActivity(intent);
}
private String getImagePathInAssets(String item) {
        return bookInfo.bookFileName + "/" + item;
    }

    private void share(String item) {
        Uri uri = Uri.parse("content://com.captainhappiness/" + getImagePathInAssets(item));
        SocialUtils.shareImage(context, uri);
    }

This is what I found in gmail logs:

这是我在gmail日志中找到的:

W/Gmail: No itemId found for event forward
03-13 14:30:32.448 12507-12507/? W/Gmail: Error opening file to obtain size.
03-13 14:30:32.448 12507-12507/? W/Gmail: java.io.FileNotFoundException: Not a whole file
03-13 14:30:32.448 12507-12507/? W/Gmail:     at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:820)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:758)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at dma.a(SourceFile:481)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at dma.a(SourceFile:441)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at cmk.a(SourceFile:3756)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at cmt.run(SourceFile:23709)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at cmk.a(SourceFile:23689)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at cmk.a(SourceFile:1405)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at cmk.K(SourceFile:1542)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at cmk.onCreate(SourceFile:31759)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at com.google.android.gm.ComposeActivityGmail.onCreate(SourceFile:225)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at android.app.Activity.performCreate(Activity.java:6093)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1115)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2566)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2677)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at android.app.ActivityThread.access$800(ActivityThread.java:176)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1529)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at android.os.Handler.dispatchMessage(Handler.java:111)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at android.os.Looper.loop(Looper.java:194)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at android.app.ActivityThread.main(ActivityThread.java:5747)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at java.lang.reflect.Method.invoke(Native Method)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at java.lang.reflect.Method.invoke(Method.java:372)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1104)
03-13 14:30:32.448 12507-12507/? W/Gmail:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
03-13 14:30:32.450 12507-12507/? W/Gmail: Error adding attachment - empty attachment

2 个解决方案

#1


0  

You can try;

你可以试试;

shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

For further info please look at the developer portal; https://developer.android.com/training/sharing/send.html

有关详细信息,请查看开发人员门户网站; https://developer.android.com/training/sharing/send.html

#2


0  

I found the solution. I used this lib and it works well. https://github.com/commonsguy/cwac-provider I think I need to implement getType and query methods and do some stuff there to make it work. But I decided to use the lib instead.

我找到了解决方案。我用过这个lib,效果很好。 https://github.com/commonsguy/cwac-provider我想我需要实现getType和查询方法,并在那里做一些事情来使它工作。但我决定改用lib。

#1


0  

You can try;

你可以试试;

shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

For further info please look at the developer portal; https://developer.android.com/training/sharing/send.html

有关详细信息,请查看开发人员门户网站; https://developer.android.com/training/sharing/send.html

#2


0  

I found the solution. I used this lib and it works well. https://github.com/commonsguy/cwac-provider I think I need to implement getType and query methods and do some stuff there to make it work. But I decided to use the lib instead.

我找到了解决方案。我用过这个lib,效果很好。 https://github.com/commonsguy/cwac-provider我想我需要实现getType和查询方法,并在那里做一些事情来使它工作。但我决定改用lib。