我正在尝试使用其他应用程序打开私有文件。我创建了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" />
答案 0 :(得分:0)
您尚未为所需的ACTION_VIEW
和(格式错误的)MIME类型实施支持Uri
的活动。 ContentProvider
不是Activity
。
答案 1 :(得分:0)