No such file or directory in Android 10 (api 29)

前端 未结 3 1106

I am working on a photo editor app in which after editing my picture I save it into my local storage. It is working fine till android 9 but not on android 10. It shows excep

相关标签:
3条回答
  • 2021-01-13 12:33

    Here is the best I could find: https://developer.android.com/training/data-storage/app-specific#external

    Basically, you now use app-specific directories for your files. For example:

    @Nullable
    File getAppSpecificAlbumStorageDir(Context context, String albumName) {
        // Get the pictures directory that's inside the app-specific directory on
        // external storage.
        File file = new File(context.getExternalFilesDir(
                Environment.DIRECTORY_PICTURES), albumName);
        if (file == null || !file.mkdirs()) {
            Log.e(LOG_TAG, "Directory not created");
        }
        return file;
    }
    
    0 讨论(0)
  • 2021-01-13 12:40

    If you target Android 10 (API level 29) or higher, set the value of requestLegacyExternalStorage to true in your app's manifest file:

    Documentation

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.appname"
        android:installLocation="auto">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:requestLegacyExternalStorage="true"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme.NoActionBar">
    
            <activity android:name=".activities.MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
    
        </application>
    
    </manifest>
    
    0 讨论(0)
  • 2021-01-13 12:54

    I found this working for me. i am trying to list all file on ListView

    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
    
        } else {
    
    
            File s = Environment.getExternalStorageDirectory();
            Log.d("Path ",Arrays.toString(s.listFiles()));
            File[] s1 = new File[s.listFiles().length];
    
            for(int i=0;i<s.listFiles().length;i++){
                s1[i]= new File(s.listFiles()[i].toString().replace("/storage/emulated/0/", ""));
            }
    
    
    
            ArrayAdapter<File> adapter = new ArrayAdapter<File>(this,R.layout.support_simple_spinner_dropdown_item,s1);
            l1.setAdapter(adapter);
    
         }
    
    0 讨论(0)
提交回复
热议问题