创建文件:没有此类文件或目录android

时间:2019-01-26 08:18:41

标签: android file android-studio

我想做的事情很简单:从手机(任何地方)中选择一张图像并将其转换为File。我的最终目标是使用此文件将此文件上传到我的数据库中(这是我用相机拍摄的照片已经成功完成的事情)。

我的情况是:当我尝试从Uri给定的路径创建文件时。没用我从SO和其他一些网站上的一些主题中读到,有时候像这些网站上一样,无法通过Uri的路径创建文件:

https://freakycoder.com/android-notes-73-how-to-get-real-path-from-uri-2f78320987f5

https://www.dev2qa.com/how-to-get-real-file-path-from-android-uri/

也尝试过这种方法:Android - Get file with path

但仍然无效。

我当前的代码: 我如何拍照:

public void exportPicture() {

        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        startActivityForResult(intent, READ_REQUEST_CODE);
    }

我的Uri如何充实:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            if (data != null) {
                photoURI = data.getData();
                Log.d("URI", "Uri:" + photoURI.toString());
            }
        }
    }

我如何尝试创建文件:

if (photoURI != null) {
  File file = new File (photoURI.getPath());
}

我遇到以下错误:

  

java.io.FileNotFoundException:/ external / file / 186142(没有这样的文件或   目录)

我在奥利奥之下。

编辑:在这里找到答案:stackoverflow.com/a/52095352

0 个答案:

没有答案