gpt4 book ai didi

android - 在 Android 11 中使用下载管理器以及分区存储的影响

转载 作者:行者123 更新时间:2023-12-05 03:39:25 25 4
gpt4 key购买 nike

我在做什么

  • 我正在尝试使用下载管理器将文件下载到设备内部存储
  • 内部存储指的是位置 /storage/emulated/0/Android/data/com.example.code/files/Download/Devrath/test.db
  • 现在我正在使用下面的模拟器,代码可以正常工作并成功下载文件

问题

  1. 分区存储会影响 future 吗?
  2. 或者既然下载管理器是一个系统服务,它会继续工作

list

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

渐变

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 项目。
  • 在此处发帖以帮助他人。
  • 我有一个完整的工作解决方案

GIT-HUB - Link

关于android - 在 Android 11 中使用下载管理器以及分区存储的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68585815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com