在 Android 11 中使用下载管理器以及分区存储的影响
Using download manager in andorid 11 and the impact of scoped storage
我在做什么
- 我正在尝试使用下载管理器将文件下载到设备
内部存储
- 内部存储指的是位置
/storage/emulated/0/Android/data/com.example.code/files/Download/Devrath/test.db
- 现在我使用的是模拟器,下面是可以正常工作并成功下载文件的代码
问题
- 将来分区存储会影响这个吗?
- 或者下载管理器是系统服务,会不会继续
工作
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.code">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:usesCleartextTraffic="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Code">
<activity android:name=".MainActivityTwo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
gradle
compileSdkVersion 31
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.code"
minSdkVersion 16
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
MainActivityTwo
public class MainActivityTwo extends AppCompatActivity {
private static final int REQUEST_CODE = 100;
//public static final String imageURL = "http://www.tutorialspoint.com/java/java_tutorial.pdf";
public static final String imageURL = "http://speedtest.ftp.otenet.gr/files/test10Mb.db";
// String imageName = "java_tutorial.pdf";
String filePath = "";
String imageName = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
filePath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString()
.concat(File.separator)
.concat("Devrath").concat(File.separator);
imageName = "test.db";
// storage runtime permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
}
}
Button btnDownloadImage = findViewById(R.id.initiateDownloadId);
Button chkIfFileExistsId = findViewById(R.id.chkIfFileExistsId);
btnDownloadImage.setOnClickListener(v -> downloadImage(imageURL, imageName));
chkIfFileExistsId.setOnClickListener(v -> checkIfFileExists());
}
public void downloadImage(String url, String outputFileName) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(imageName);
request.setDescription("Downloading " + imageName);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.allowScanningByMediaScanner();
//request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, outputFileName); // ---> Working
//request.setDestinationInExternalFilesDir(this, Environment.getExternalStorageDirectory().toString() + File.separator, outputFileName);
request.setDestinationInExternalFilesDir(this,filePath, outputFileName);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
private void checkIfFileExists() {
//-> /storage/emulated/0/Android/data/com.example.code/files/Download/Devrath/test.db
File mFile = new File(filePath.concat(imageName));
if(mFile.exists()){
Toast.makeText(this,"File exists",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this,"File does not exists",Toast.LENGTH_LONG).show();
}
}
}
- 我最终使用了应用程序的内部存储...我创建了一个 GitHub 项目。
- 在这里发帖,希望对某人有所帮助。
- 我有一个完整的工作解决方案
我在做什么
- 我正在尝试使用下载管理器将文件下载到设备 内部存储
- 内部存储指的是位置
/storage/emulated/0/Android/data/com.example.code/files/Download/Devrath/test.db
- 现在我使用的是模拟器,下面是可以正常工作并成功下载文件的代码
问题
- 将来分区存储会影响这个吗?
- 或者下载管理器是系统服务,会不会继续 工作
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.code">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:usesCleartextTraffic="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Code">
<activity android:name=".MainActivityTwo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
gradle
compileSdkVersion 31
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.code"
minSdkVersion 16
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
MainActivityTwo
public class MainActivityTwo extends AppCompatActivity {
private static final int REQUEST_CODE = 100;
//public static final String imageURL = "http://www.tutorialspoint.com/java/java_tutorial.pdf";
public static final String imageURL = "http://speedtest.ftp.otenet.gr/files/test10Mb.db";
// String imageName = "java_tutorial.pdf";
String filePath = "";
String imageName = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
filePath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString()
.concat(File.separator)
.concat("Devrath").concat(File.separator);
imageName = "test.db";
// storage runtime permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
}
}
Button btnDownloadImage = findViewById(R.id.initiateDownloadId);
Button chkIfFileExistsId = findViewById(R.id.chkIfFileExistsId);
btnDownloadImage.setOnClickListener(v -> downloadImage(imageURL, imageName));
chkIfFileExistsId.setOnClickListener(v -> checkIfFileExists());
}
public void downloadImage(String url, String outputFileName) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(imageName);
request.setDescription("Downloading " + imageName);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.allowScanningByMediaScanner();
//request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, outputFileName); // ---> Working
//request.setDestinationInExternalFilesDir(this, Environment.getExternalStorageDirectory().toString() + File.separator, outputFileName);
request.setDestinationInExternalFilesDir(this,filePath, outputFileName);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
private void checkIfFileExists() {
//-> /storage/emulated/0/Android/data/com.example.code/files/Download/Devrath/test.db
File mFile = new File(filePath.concat(imageName));
if(mFile.exists()){
Toast.makeText(this,"File exists",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this,"File does not exists",Toast.LENGTH_LONG).show();
}
}
}
- 我最终使用了应用程序的内部存储...我创建了一个 GitHub 项目。
- 在这里发帖,希望对某人有所帮助。
- 我有一个完整的工作解决方案