Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android9+:需要加入处理scoped storage #8

Closed
emclab opened this issue Feb 3, 2022 · 1 comment
Closed

Android9+:需要加入处理scoped storage #8

emclab opened this issue Feb 3, 2022 · 1 comment

Comments

@emclab
Copy link

emclab commented Feb 3, 2022

android9/10用的是scoped storage,每个应用存储在自己的子目录下。而Android9以前,应用是共享一个存储子目录。本OSS库不知道scoped storage,而是仍然用共享存储,因此在Android10上出错。以下是关于EXTERNAL_STORAGE的文章

if (android.os.Build.VERSION.SDK_INT < 29) {

 // ==>  /storage/emulated/0    (Emulator)
 File dir = Environment.getExternalStorageDirectory();

} else if (android.os.Build.VERSION.SDK_INT >= 29) {
// ==> /storage/emulated/0/Android/data/org.o7planning.externalstoragedemo/files
File dir = this.getExternalFilesDir(null);
}
本库对全部Android版本都仅仅使用了Environment.getExternalStorageDirectory();。这也就是为何在错误信息中显示的是共享路径(Android9以前)的原因。本库的AliOssUploadManager.java和FileUtils.java需要update。或许还有其他的也需要update。
以下是aliyun_oss_react-native下的两个issue:

  1. 上传图像在Android emulator运行通畅但在安桌手机上stuck aliyun/aliyun-oss-react-native#90
  2. 公告:这个SDK后面有时间准备重新搞起来了,大家有issue,有PR的欢迎可以提交 aliyun/aliyun-oss-react-native#88
@emclab
Copy link
Author

emclab commented Feb 3, 2022

the following code change in FileUtils.java works in Android 10 emulator:

import android.os.Build;
public static String getFilePathFromURI(Context context, Uri contentUri) {
        //copy file and send new file path
        String fileName = getFileName(contentUri);
        if (!TextUtils.isEmpty(fileName)) {
            File copyFile;
            if (Build.VERSION.SDK_INT >= 29) {
            // ==> /storage/emulated/0/Android/data/org.o7planning.externalstoragedemo/files
                copyFile = new File( context.getExternalFilesDir(null).getAbsolutePath() + File.separator + fileName);
            } else {
                copyFile = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + fileName);
            }
            
            FileUtils.copy(context, contentUri, copyFile);
            return copyFile.getAbsolutePath();
        }
        return null;
    }

@emclab emclab closed this as completed Feb 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant