簡體   English   中英

Android 10 (api 29) 中沒有這樣的文件或目錄

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

我正在開發一個照片編輯器應用程序,在其中編輯我的圖片后,我將它保存到我的本地存儲中。 It is working fine till android 9 but not on android 10. It shows exception of "No such file or directory found" in Android 10. After some research I found that getExternalFilesDir() is deprecated in android Q+. 但是我在 android 10 中找不到任何合適的方法。所以如果有人可以提供教程,那將非常有幫助。

我已經添加並授予使用權限 android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 以防萬一這是問題,但它沒有解決任何問題。

這是我的嘗試(使用 ParcelFileDescriptor):

private void fileAccessForAndroidQ(Uri fileUri){
    try {
        ParcelFileDescriptor parcelFileDescriptor = this.getContentResolver().openFileDescriptor(fileUri, "r", null);
        InputStream inputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
        Cursor returnCursor =
                getContentResolver().query(fileUri, null, null, null, null);
        int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        returnCursor.moveToFirst();
        fileName = returnCursor.getString(nameIndex);

        file = new File(this.getFilesDir(), fileName);

        OutputStream outputStream = new FileOutputStream(file);
        IOUtils.copyStream(inputStream, outputStream);

    }catch (Exception e){
        Toast.makeText(this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

任何形式的幫助將不勝感激。

如果您以 Android 10(API 級別 29)或更高版本為目標,請在應用的清單文件中將requestLegacyExternalStorage的值設置為true

文檔

<?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>

這是我能找到的最好的: https://developer.android.com/training/data-storage/app-specific#external

基本上,您現在為您的文件使用特定於應用程序的目錄。 例如:

@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;
}

我發現這對我有用。 我正在嘗試列出 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);

     }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM