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



我正在创建一个简单的应用程序来拍照。这是我的代码

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);
    }

}

正如你所看到的,我在最后添加了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)

猜猜这个目录是否存在?孢子虫警报,确实如此。并且图像并不是在BitmapFactory.decodeFile之后创建的。我真的不明白我做错了什么。一切都很好,只是当它真的必须显示照片时,它就不显示了。只是空白。就像WTF m8一样,我只是努力做好我的工作,不需要发疯,你知道的。

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

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

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

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

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

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

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

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

对我来说,是文件路径错误,所以我需要获得真正的文件路径。

第一个

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;
}

然后返回到Uri

Uri newUri = Uri.fromFile(file);

转换到文件并返回到uri对我来说很有用。我从操作中收到了简单的数据。发送。

最新更新