Android 10(api 29)中没有此类文件或目录 [英] No such file or directory in Android 10 (api 29)

查看:282
本文介绍了Android 10(api 29)中没有此类文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用照片编辑器应用程序,在其中编辑图片后,将其保存到本地存储中.直到android 9都可以正常工作,但是在android 10上却不能.它显示了Android 10中找不到此类文件或目录"的异常.经过一番研究,我发现android Q +中不赞成使用getExternalFilesDir().但是我无法在android 10中找到任何合适的方法来完成它.因此,如果有人可以提供教程,那将非常有帮助.

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.

我已经添加并授予了uses-permission android:name ="android.permission.WRITE_EXTERNAL_STORAGE"/>,以防万一,这并没有解决任何问题.

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.

这是我的尝试(使用ParcelFileDescriptor):

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();
    }
}

任何帮助将不胜感激.

推荐答案

如果您定位的是Android 10(API级别29)或更高版本,请在应用的清单文件中将requestLegacyExternalStorage的值设置为true:

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

文档

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

这篇关于Android 10(api 29)中没有此类文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆