Par*_*ani 5 java android android-studio
我想将文件从我的 url 下载并保存到 Android 10 中的外部存储下载目录。我已使用此代码进行下载。
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);
}
Run Code Online (Sandbox Code Playgroud)
它显示错误
E/AndroidRuntime:致命异常:主进程:com.example.abc,PID:15197
Run Code Online (Sandbox Code Playgroud)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)
但如果我使用
request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOWNLOADS+ File.separator , "parag.jpeg");
Run Code Online (Sandbox Code Playgroud)
这会下载文件,但在我的应用程序特定文件夹中,但我想在外部公共下载目录中下载文件
根据您的代码,不推荐使用getExternalStoragePublicDirectory API。
使用以下代码将文件保存在标准目录中。
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES,"parag.jpeg")
Run Code Online (Sandbox Code Playgroud)
它将尝试保存到标准目录
/storage/emulated/0/Android/data/packagename/files/Pictures/parag.jpg
Run Code Online (Sandbox Code Playgroud)
对于针对 Build.VERSION_CODES.Q 或更高版本的应用程序,不需要 WRITE_EXTERNAL_STORAGE 权限,并且 dirType 必须是已知的公共目录之一,如 Environment#DIRECTORY_DOWNLOADS、Environment#DIRECTORY_PICTURES、Environment#DIRECTORY_MOVIES 等。
我希望这有帮助。
首先将此行添加到应用程序标记内的 AndroidManifest.xml 文件中。
android:requestLegacyExternalStorage="true"
Run Code Online (Sandbox Code Playgroud)
使用Environment.DIRECTORY_PICTURES或任何适合您的文件的环境。
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
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6476 次 |
| 最近记录: |