问题:在Android 10中访问文件时遇到问题 – Android Studio

huangapple go评论73阅读模式
英文:

problem having accessing files in android 10 :- android studio

问题

Android 10不允许我从外部存储访问我的文件。在Android 9中,我的应用程序运行正常。我将我的手机从Android 9升级到Android 10后,我的应用程序停止响应。

main_activity.java

File directory;
File path;
File[] listfile;
ArrayList<String> arrayList;

directory = Environment.getExternalStorageDirectory();

path = new File(directory.getAbsolutePath() + "/myfile/");

arrayList = new ArrayList<String>();
mycardview.setAdapter(new ImageAdapter(listimage.this, arrayList));

if (path.isDirectory()) {
    listfile = path.listFiles();
    for (int i = 0; i < listfile.length; i++) {
        arrayList.add(listfile[i].getAbsolutePath());
    }
    Collections.shuffle(arrayList);
}

**错误**

> java.lang.RuntimeException: 无法启动活动
> ComponentInfo{com.example.rajiv/com.example.rajiv.listimage}:
> java.lang.NullPointerException: 尝试获取null数组的长度
>         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3374)
>         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3513)
>         at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
>         at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
>         at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
>         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2109)
>         at android.os.Handler.dispatchMessage(Handler.java:107)
>         at android.os.Looper.loop(Looper.java:214)
>         at android.app.ActivityThread.main(ActivityThread.java:7682)
>         at java.lang.reflect.Method.invoke(Native Method)
>         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
>         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
>      Caused by: java.lang.NullPointerException: 尝试获取null数组的长度
>         at com.example.rajiv.listimage.onCreate(listimage.java:74)
>         at android.app.Activity.performCreate(Activity.java:7815)
>         at android.app.Activity.performCreate(Activity.java:7804)
>         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1318)
>         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3349)

我已经在互联网上检查过,问题不在代码中。Google在Android 10中应用了安全补丁,因此代码不起作用。我还在清单中使用了权限以及运行时权限...

英文:

Android 10 is not letting me access my files from external storage. In android 9 my app was working fine.I updated my phone from android 9 to android 10 and my app stopped responding.

main_activity.java

File directory;
File path;
File[] listfile;
//File listFiles;
ArrayList&lt;String&gt; arrayList;
directory = Environment.getExternalStorageDirectory();
path = new File(directory.getAbsolutePath()+ &quot;/myfile/&quot;);
arrayList = new ArrayList&lt;String&gt;();
mycardview.setAdapter(new ImageAdapter(listimage.this, arrayList));
if (path.isDirectory()) {
listfile =path.listFiles();
for (int i = 0; i &lt; listfile.length; i++) {
arrayList.add(listfile[i].getAbsolutePath());
}
Collections.shuffle(arrayList);

ERROR :

> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.example.rajiv/com.example.rajiv.listimage}:
> java.lang.NullPointerException: Attempt to get length of null array
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3374)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3513)
> at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
> at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
> at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2109)
> at android.os.Handler.dispatchMessage(Handler.java:107)
> at android.os.Looper.loop(Looper.java:214)
> at android.app.ActivityThread.main(ActivityThread.java:7682)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
> Caused by: java.lang.NullPointerException: Attempt to get length of null array
> at com.example.rajiv.listimage.onCreate(listimage.java:74)
> at android.app.Activity.performCreate(Activity.java:7815)
> at android.app.Activity.performCreate(Activity.java:7804)
> at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1318)
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3349)

I have checked on internet the problem is not in code
google have applied a security patch in android 10 so the code isnt working i have also used permission on manifest as well as runtime permission ..

答案1

得分: 0

又有人被Google的更改所困扰。

请查看https://developer.android.com/training/data-storage

您需要使用MediaStore或Storage Access Framework(SAF),有关文件位于您的应用程序私有目录之外的详细信息,请参阅https://developer.android.com/training/data-storage/shared。

不过,作为一个快速解决方法,可以暂时选择退出https://developer.android.com/training/data-storage/compatibility,但这只能在Android 11之前起作用。

英文:

Another one caught out by Google changes.

See https://developer.android.com/training/data-storage

You need to use MediaStore or Storage Access Framework (SAF), details https://developer.android.com/training/data-storage/shared for files outside of your App's private directories.

Though as a quick fix is to temporarily opt out https://developer.android.com/training/data-storage/compatibility but this will only work until Android 11

huangapple
  • 本文由 发表于 2020年1月4日 00:24:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定