android - 不幸的是,相机在使用 Content Provider 时停止了”

标签 android crash android-contentprovider android-camera-intent

enter image description here如果我在我的应用程序中使用 ContentProvider,我会在 Result_ok 后收到类似“不幸的是相机已停止”的错误。这是我的代码:

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        i.putExtra(MediaStore.EXTRA_OUTPUT, MyFileContentProvider.CONTENT_URI);
 startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

如何解决这个问题?我不想将图像保存到 SD 卡中。

这是我的 MyFileContentProvider 类:

public class MyFileContentProvider extends ContentProvider {

    public static final Uri CONTENT_URI = Uri.parse("content://com.example.user.studentadmission/");
    private static final HashMap<String, String> MIME_TYPES = new HashMap<String, String>();
    static {
        MIME_TYPES.put(".jpg", "image/jpeg");
        MIME_TYPES.put(".jpeg", "image/jpeg");
    }
    @Override
    public boolean onCreate() {
        try {
            File mFile = new File(getContext().getFilesDir(), "student.jpg");
            if(!mFile.exists()) {
                mFile.createNewFile();
            }
            getContext().getContentResolver().notifyChange(CONTENT_URI, null);
            return (true);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public String getType(Uri uri) {
        String path = uri.toString();
        for (String extension : MIME_TYPES.keySet()) {
            if (path.endsWith(extension)) {
                return (MIME_TYPES.get(extension));
            }
        }
        return (null);
    }

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode)
            throws FileNotFoundException {
        File f = new File(getContext().getFilesDir(), "student.jpg");
        if (f.exists()) {
            return (ParcelFileDescriptor.open(f,
                    ParcelFileDescriptor.MODE_READ_WRITE));
        }
        throw new FileNotFoundException(uri.getPath());
    }

    @Override
    public Cursor query(Uri url, String[] projection, String selection,
                        String[] selectionArgs, String sort) {
        throw new RuntimeException("Operation not supported");
    }

    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {
        throw new RuntimeException("Operation not supported");
    }

    @Override
    public int update(Uri uri, ContentValues values, String where,
                      String[] whereArgs) {
        throw new RuntimeException("Operation not supported");
    }

    @Override
    public int delete(Uri uri, String where, String[] whereArgs) {
        throw new RuntimeException("Operation not supported");
    }

} 

最佳答案

很少有相机 Action 只适用于Kitkat, Action 使用条件:

Intent i = (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
                        ? new Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE)
                        : new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

i.putExtra(MediaStore.EXTRA_OUTPUT, MyFileContentProvider.CONTENT_URI);
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

如果仍然发生崩溃,请尝试避免仅用于测试的 ContentProvider。让我知道。

关于android - 不幸的是,相机在使用 Content Provider 时停止了”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39524034/

相关文章:

android - 如何在不存储在 SD 卡的情况下发送图像?

android - 收听 Intent ACTION_VIEW 的 Activity

ios - 与 iOS 6 一起使用的 AdMob 使应用程序崩溃(无法识别的选择器)

objective-c - presentViewController : crash on iOS <6 (AutoLayout)

android - 删除记录时Android崩溃

android - 如何修复在 android 内容提供者中找不到的内容提供者 url?

android - Eclipse 中的主题预览失败

android - 从 Android Studio 风格设置 process.env 变量

android - 将文本编辑为字符串

android - 如何获取平板电脑上所有用户的内容提供者列表