无法使用Gmail

时间:2017-03-13 12:11:46

标签: android android-contentprovider

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

这是我的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);成功运行。

这是我分享图片的方式:

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);
    }

这是我在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 个答案:

答案 0 :(得分:0)

你可以试试;

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

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

答案 1 :(得分:0)

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