从uri获取位图时获取文件未找到异常

时间:2014-01-22 08:53:25

标签: android bitmap mediastore

我试图获取URI的图像位图,但它给了我以下错误。

System.err(3102):java.io.FileNotFoundException:没有内容提供者:/storage/emulated/0/1.jpg

我尝试的代码是,

    Cursor mCursor;
    mCursor=activity.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,mProjection,null, null, MediaStore.Images.Media.DEFAULT_SORT_ORDER);

    if (mCursor != null) {
        mCursor.moveToFirst();
        do {
            String strPath = mCursor.getString(mCursor
                    .getColumnIndex(MediaStore.Images.Media.DATA));
                        try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(
                        activity.getContentResolver(), Uri.parse(strPath));
                                   //Here i'm using bitmap
                        } catch (FileNotFoundException e) {
                Log.i("Exception",
                        "Image file not found: " + e.getMessage());
                e.printStackTrace();
                         } catch (IOException e) {
                Log.i("Exception",
                        "Input Output Failure: " + e.getMessage());
                e.printStackTrace();
             }

        } while (mCursor.moveToNext());

2 个答案:

答案 0 :(得分:0)

从uri获取路径并使用此路径获取图像

public String getRealPathFromURI(Uri contentUri) {
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } catch (Exception e) {
        return contentUri.getPath();
    }
}

答案 1 :(得分:0)

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
         if (requestCode == 2 && resultCode == RESULT_OK) {

            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            File file = new File(picturePath);
            Bitmap bmpp = decodeAndResizeFile(file);
// get bitmap that which image select you and set bmpp to imageview 

        }
相关问题