aliyun / aliyun-oss-react-native

MIT License
148 stars 99 forks source link

android No such file or directory #50

Open oldfeel opened 5 years ago

oldfeel commented 5 years ago

我剪切图片后上传时报错 No such file or directory,发现原因是 getFilePathFromURI 方法中没有正确获取缓存中的图片地址.

原图片地址

file:///storage/emulated/0/Android/data/com.awesomeapp/cache/1554800192263.jpg

通过 getFilePathFromURI 获取的图片地址

file:///storage/emulated/0/1554800192263.jpg // No such file or directory

应该直接把 file:// 去掉就能使用了.

    public static String getFilePathFromURI(Context context, Uri contentUri) {
        Log.d("oldfeel", "getFilePathFromURI: " + contentUri);
        //copy file and send new file path
        if(contentUri.toString().contains(context.getApplicationContext().getPackageName())){
            return contentUri.toString().substring(6);
        }

        String fileName = getFileName(contentUri);
        if (!TextUtils.isEmpty(fileName)) {
            File copyFile = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + fileName);
            FileUtils.copy(context, contentUri, copyFile);
            return copyFile.getAbsolutePath();
        }
        return null;
    }
Pzz0912 commented 5 years ago

你好,我也有这个问题 我把file://去掉也是无法上传

不过我使用android 5.1版本的手机上传的时候正常,最新版本的却报这个问题,请问还有其他的解决方法吗?

iShawnWang commented 4 years ago

还是某些手机偶现, 愁人 !!!

Kuhne1 commented 4 years ago

The error is to do with permissions To use this library you must have the user accept storage permissions Example with no permissions: Error: /storage/emulated/0/bfd0e85803820e51ecf276d7dc96ab53e13f0a4aff8730f7010eb0c0b13fef37.jpg (No such file or directory)

With permissions, it should work.

Hope this helps

iShawnWang commented 4 years ago

未解决, 复现的情况是 自己拍照的照片没问题... 微信等三方 App 保存到相册中, 用这个库 上传, 就会有如上报错

szj2935 commented 3 years ago

遇到同样的问题, 将getFilePathFromURI里的 Environment.getExternalStorageDirectory() 修改为 context.getExternalFilesDir(null),得到解决

yuyueguan-zhao commented 2 years ago

我传进去的是file:///storage/emulated/0/Android/data/com.awesomeapp/cache/1554800192263.jpg 但是返回/storage/emulated/0/1554800192263.jpg // No such file or directory 请问要如何解决呢?是我没有获取到真实路径吗?还是其他的呢?

cuantmac commented 2 years ago

我传进去的是file:///storage/emulated/0/Android/data/com.awesomeapp/cache/1554800192263.jpg 但是返回/storage/emulated/0/1554800192263.jpg // No such file or directory 请问要如何解决呢?是我没有获取到真实路径吗?还是其他的呢?

+1