java下载时above缺失,java.lang.IllegalStateException: Not one of standard directories: /storage/emulated/0...

原文:

I Want To Download And save file from my url to external storage download directory in Android 10.

I have used this code to download.

public void StartNewDownload(String url) {

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

request.setDescription("Downloading Profile");

request.setTitle("Abc App");

request.setDestinationInExternalPublicDir(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+ File.separator, "parag.jpeg");

DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

final long downloadId = manager.enqueue(request);

}

it shows error

E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.abc, PID: 15197

java.lang.IllegalStateException: Not one of standard directories: /storage/emulated/0/Download/

at android.os.Parcel.createException(Parcel.java:2079)

at android.os.Parcel.readException(Parcel.java:2039)

at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)

at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)

at android.content.ContentProviderProxy.call(ContentProviderNative.java:658)

at android.content.ContentProviderClient.call(ContentProviderClient.java:558)

at android.content.ContentProviderClient.call(ContentProviderClient.java:546)

at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:569)

at com.example.abc.Activity.showDetails.StartNewDownload(showDetails.java:102)

at com.example.abc.Activity.showDetails$1.onClick(showDetails.java:68)

But if i use

request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOWNLOADS+ File.separator , "parag.jpeg");

this downloads the file but in my app specific folder

but i want to download the file in external public download directory

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

as per your code getExternalStoragePublicDirectory API is deprecated.

use the below code to save the file in the standard directory.

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES,"parag.jpeg")

it will try to save to the standard directory

/storage/emulated/0/Android/data/packagename/files/Pictures/parag.jpg

For applications targeting Build.VERSION_CODES.Q or above,

WRITE_EXTERNAL_STORAGE permission is not needed and the dirType must

be one of the known public directories like

Environment#DIRECTORY_DOWNLOADS, Environment#DIRECTORY_PICTURES,

Environment#DIRECTORY_MOVIES, etc.

I hope this helps.

# Answer 2

First of all add this line to your AndroidManifest.xml file inside application tag.

android:requestLegacyExternalStorage="true"

Use Environment.DIRECTORY_PICTURES or any Environment that Appropriate to your file.

request.setAllowedNetworkTypes(

DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE

)

.setAllowedOverRoaming(false).setTitle("Krishan Demo") //Download Manager Title

.setDescription("Downloading...") // Download manager Discription

.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)

.setDestinationInExternalPublicDir(

Environment.DIRECTORY_PICTURES, // It can be any stanaderd directory Like DCIM,Downloads etc...

"/KrishanImages/testing.jpg" // Your Custom directory name/Your Image file name

)

# Answer 3

You can use the code below to save the file.

request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOWNLOADS, fileType)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值