BitmapFactory:无法解码流:java.io.FileNotFoundException 即使文件确实存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36242457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:36:46  来源:igfitidea点击:

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

javaandroid

提问by Leo300

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;


private File createTemporaryFile(String part, String ext) throws Exception {


    File externalStorageDirectory = Environment.getExternalStorageDirectory();
    File tempDir = new File(externalStorageDirectory + "/cameratest/");
    if (!tempDir.exists()) {
        tempDir.mkdir();

    }
    return File.createTempFile(part, ext, tempDir);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    b1 = (Button) findViewById(R.id.button);
    iv = (ImageView) findViewById(R.id.imageView);

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

            try {
                // place where to store camera taken picture
                photo = createTemporaryFile("picture", ".jpg");
                photo.delete();
            } catch (Exception e) {
                Log.v(TAG, "Can't create file to take picture!");
                Toast.makeText(getApplicationContext(), "Please check SD card! Image shot is impossible!",
                        Toast.LENGTH_SHORT).show();

            }

            mImageUri = Uri.fromFile(photo);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);

            startActivityForResult(intent, 0);
        }
    });
}

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


    if (requestCode == 0 && resultCode == RESULT_OK) {



        Log.d(TAG, mImageUri.toString());
        Bitmap bitmap = BitmapFactory.decodeFile(mImageUri.toString());
        iv.setImageBitmap(bitmap);

    }


}

as you can see i've added eLog.d(TAG, mImageUri.toString());at the end and in the logcat (as well as the FileNotFoundException) i see this direcory:

正如你所看到的,我eLog.d(TAG, mImageUri.toString());在最后和 logcat(以及FileNotFoundException)中添加了这个目录:

03-27 00:43:30.498 30526-30526/myapplication.example.falcoleo.cameratest1 D/MAIN?ACTIVITY: file:///storage/emulated/0/cameratest/picture459838058.jpg
03-27 00:43:30.499 30526-30526/myapplication.example.falcoleo.cameratest1 E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: file:/storage/emulated/0/cameratest/picture459838058.jpg: open failed: ENOENT (No such file or directory)

guess if this directory exists? spoler alert, it does. And it's not like the image is created after the BitmapFactory.decodeFile. I really do not understand what i'm doing wrong. Everything works fine except when it actually has to display the photo, then it just does not display it. just blank. Like WTF m8 i'm just trying to do my job no need to go crazy, you know.

猜猜这个目录是否存在? spoler 警报确实如此。它不像图像是在BitmapFactory.decodeFile. 我真的不明白我做错了什么。一切正常,除非它实际上必须显示照片,然后它只是不显示它。只是空白。就像WTF m8,我只是在努力做好我的工作,不需要发疯,你知道。

采纳答案by F43nd1r

Replace mImageUri.toString()with mImageUri.getPath().

替换mImageUri.toString()mImageUri.getPath()

decodeFileexpects a path, not an uri string.

decodeFile需要一个路径,而不是一个 uri 字符串。

回答by greenapps

file:///storage/emulated/0/cameratest/picture459838058.jpg

Remove file://because the decodeFile() expects a file system path.

删除,file://因为 decodeFile() 需要文件系统路径。

/storage/emulated/0/cameratest/picture459838058.jpg

回答by Praful C

Use BitmapFactory.decodeStream instead of BitmapFactory.decodeFile.

使用 BitmapFactory.decodeStream 而不是 BitmapFactory.decodeFile。

try ( InputStream is = new URL( file_url ).openStream() ) {
  Bitmap bitmap = BitmapFactory.decodeStream( is );
}

Source https://stackoverflow.com/a/28395036/5714364

来源https://stackoverflow.com/a/28395036/5714364

回答by DragonFire

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.

这种转换为文件并返回到 uri 对我有用。我正在从 action.SEND 接收简单的数据。