【问题标题】:BitmapFactory: Unable to decode stream: java.io.FileNotFoundException even when file IS actually thereBitmapFactory:无法解码流:java.io.FileNotFoundException,即使文件实际存在
【发布时间】:2016-03-27 00:21:44
【问题描述】:

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

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(以及 FileNotFoundException)中添加了 eLog.d(TAG, mImageUri.toString());,我看到了这个目录:

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)

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

【问题讨论】:

  • 你确定你有权限在外部存储器中读/写吗?
  • 我有权限

标签: java android


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    file:///storage/emulated/0/cameratest/picture459838058.jpg
    

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

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

    【讨论】:

    • 好的,它可以工作。我用string.replace("file://","")有没有更优雅的解决方案?
    【解决方案3】:

    使用 BitmapFactory.decodeStream 代替 BitmapFactory.decodeFile。

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

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

    【讨论】:

      【解决方案4】:

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

      第一

      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 newUri = Uri.fromFile(file);
      

      这种到文件并返回 uri 的转换对我有用。我收到了来自 action.SEND 的简单数据。

      【讨论】:

      • 还有许多其他复杂的系统来获取文件路径,但这是你们中的一些人可能需要进入的方向......我只是发布了一个简单的(从其他地方复制并粘贴) 以简单的方式表达我的观点
      猜你喜欢
      • 2017-07-23
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 2019-02-09
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      相关资源
      最近更新 更多