【问题标题】:android.content.ActivityNotFoundException when opening file using ContentProvider使用 ContentProvider 打开文件时出现 android.content.ActivityNotFoundException
【发布时间】:2014-06-06 17:49:22
【问题描述】:

我正在尝试使用另一个应用程序打开一个私人文件。我为以下内容创建了 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" />

【问题讨论】:

  • 首先验证您的清单文件,您的 MainActivity 名称是否正确提及?然后你的 MainActivity 扩展了活动类。

标签: java android android-intent android-contentprovider


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    看看here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多