BitmapFactory: Unable to decode stream: java.io.FileNotFoundException even when file IS actually there

前端 未结 4 1735
刺人心
刺人心 2021-01-02 04:28

I\'m creating a simple app to take a picture. this is my code

Button b1;
ImageView iv;
String TAG = \"MAIN ACTIVITY\";

File photo;
private Uri mImageUri;


         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 05:03

    Ok for me it was the file path was wrong so I needed to get the real filepath.

    First

    File file = new File(getPath(uri));
    
    public String getPath (Uri uri)
    {
        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(uri,
                                                   projection,
                                                   null,
                                                   null,
                                                   null);
        if (cursor == null)
            return null;
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String s = cursor.getString(column_index);
        cursor.close();
        return s;
    }
    

    Then Back To Uri

    Uri newUri = Uri.fromFile(file);
    

    This conversion to file and back to uri did the trick for me. I was receiving simple data from action.SEND.

提交回复
热议问题