BitmapFactory:无法解码流:java.io.FileNotFoundException即使文件IS实际存在 [英] BitmapFactory: Unable to decode stream: java.io.FileNotFoundException even when file IS actually there

查看:180
本文介绍了BitmapFactory:无法解码流:java.io.FileNotFoundException即使文件IS实际存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

    }


}

你可以看到我在最后和logcat(以及<$ c)中添加了 eLog.d(TAG,mImageUri.toString()); $ c> FileNotFoundException )我看到这个目标:

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:

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一样,我只是想做我的工作,不需要发疯,你知道。

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.

推荐答案

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

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

decodeFile expects a path, not an uri string.

这篇关于BitmapFactory:无法解码流:java.io.FileNotFoundException即使文件IS实际存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆