android no suxh file,No such file or directory in Android 10 (api 29)

作者分享了在开发照片编辑应用时遇到的问题,Android 10中`getExternalFilesDir()`被弃用,如何在API 29及以上版本保存文件的挑战。文章提供了解决方案,包括设置requestLegacyExternalStorage为true和使用app-specific directories,同时介绍了针对Android 10的正确文件访问方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 exception of "No such file or directory found" in Android 10. After some research I found that getExternalFilesDir() is deprecated in android Q+. But I cannot find any proper way to do it in android 10. So please if anyone can provide a tutorial it would be really helpful.

I've added and granted uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in case of it was the issue, and it didn't solve anything.

This is my try (Used 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();

}

}

Any kind of help would be appreciated.

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

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

package="com.example.appname"

android:installLocation="auto">

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

# Answer 2

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值