繁体   English   中英

使用ContentProvider打开文件时出现android.content.ActivityNotFoundException

[英]android.content.ActivityNotFoundException when opening file using ContentProvider

我正在尝试使用另一个应用程序打开一个私人文件。 我为以下内容创建了ContentProvider。 但是我得到以下错误:

06-05 12:57:20.791: E/AndroidRuntime(21922): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.contentprovider/com.android.contentprovider.MainActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.android.contentprovider//data/data/com.android.contentprovider/files/Sample.txt typ=/text* }

我在主要活动中的代码是:

try {
File newFile = new File("Sample.txt");
FileOutputStream fileOutputStream = openFileOutput(newFile.toString(), MainActivity.MODE_PRIVATE);
    } catch (Exception e) {
        Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
    }

    Uri uri = Uri.parse("content://com.android.contentprovider/" + this.getFilesDir().toString() + "/Sample.txt");

    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    intent.setDataAndType(uri, "/text*");
    startActivity(intent);

我的contentprovider文件:

 package com.android.contentprovider;

import java.io.File;
import java.io.FileNotFoundException;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;

public class MyProvider extends ContentProvider {

@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    File privateFile = new File(getContext().getFilesDir(), uri.getPath());
    return ParcelFileDescriptor.open(privateFile, ParcelFileDescriptor.MODE_READ_ONLY);
}

@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
    return 0;
}

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

@Override
public Uri insert(Uri arg0, ContentValues arg1) {
    return null;
}

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

@Override
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
        String arg4) {
    return null;
}

@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
    return 0;
}

}

我已经在清单中添加了提供者

<provider 
        android:name=".MyProvider" 
        android:authorities="com.android.contentprovider" 
        android:exported="true" />

您尚未实现针对所需的Uri和(格式错误的)MIME类型支持ACTION_VIEW的活动。 ContentProvider不是Activity

我的愚蠢错误是“ / video *”,应该是“ video / *”

仍然存在一些问题。 所以我使用了v4库中的FileProvider

在这里查看

暂无
暂无

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

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