gpt4 book ai didi

android - 如何下载内部存储中特定文件夹中的文件

转载 作者:行者123 更新时间:2023-12-04 15:49:26 26 4
gpt4 key购买 nike

我正在尝试在手机的内部存储中创建一个单独的文件夹,供应用程序下载文件。但该文件夹未在手机中创建。是什么原因?我的应用程序中还有另一个问题,即当我单击下载按钮时没有下载照片。

这里是下载函数

    public void download() {
for (MediaModel item : Items) {

if (item.isSelected) {

Log.d("check", "download");
final String url = item.getFullDownloadURL();
BaseDownloadTask task = FileDownloader.getImpl().create(url);
task.setListener(mFileDownloadListener)
.setPath(Environment.getDataDirectory() + "/" + Constants.STORED_FOLDER, true)
.setAutoRetryTimes(1)
.setCallbackProgressTimes(0)
.asInQueueTask()
.enqueue();

if (FileDownloader.getImpl().start(mFileDownloadListener, true)) {
item.setTaskId(task.getId());
item.setStatus(ItemStatus.DOWNLOADING);
Logging.e(TAG, "start download task: " + task.getId());
} else {
item.setTaskId(task.getId());
item.setStatus(ItemStatus.NORMAL);
Logging.e(TAG, "error download task: " + task.getId());
}
}
}
}

最佳答案

在Android studio中使用内部存储首先在manifest中添加权限像这样:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

然后使用这行代码在内部存储中创建新目录:

   File sdCardRoot = new File(Environment.getExternalStorageDirectory(), "MyProfile");

if (!sdCardRoot.exists()) {
sdCardRoot.mkdirs();
}

Log.e("check_path", "" + sdCardRoot.getAbsolutePath());

这是我的完整代码:

在此代码中检查目录是否存在,如果目录不存在则创建目录并使用 asyntask 从 url 下载图片

在这个例子中我使用了 Java 语言

代码

  MyAsyncTasks asyncTasks = new MyAsyncTasks();
asyncTasks.execute(Imageurl);

和异步类:

class MyAsyncTasks extends AsyncTask<String, String, String> {

File sdCardRoot;

@Override
protected String doInBackground(String... strings) {

HttpURLConnection urlConnection = null;
try {
URL url = new URL(strings[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();


sdCardRoot = new File(Environment.getExternalStorageDirectory(), "MyProfile");

if (!sdCardRoot.exists()) {
sdCardRoot.mkdirs();
}

Log.e("check_path", "" + sdCardRoot.getAbsolutePath());

String fileName =
strings[0].substring(strings[0].lastIndexOf('/') + 1, strings[0].length());
Log.e("dfsdsjhgdjh", "" + fileName);
File imgFile =
new File(sdCardRoot, fileName);
if (!sdCardRoot.exists()) {
imgFile.createNewFile();
}
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
FileOutputStream outPut = new FileOutputStream(imgFile);
int downloadedSize = 0;
byte[] buffer = new byte[2024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
outPut.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
Log.e("Progress:", "downloadedSize:" + Math.abs(downloadedSize * 100 / totalSize));
}
Log.e("Progress:", "imgFile.getAbsolutePath():" + imgFile.getAbsolutePath());

Log.e(TAG, "check image path 2" + imgFile.getAbsolutePath());

mImageArray.add(imgFile.getAbsolutePath());
outPut.close();
} catch (IOException e) {
e.printStackTrace();
Log.e("checkException:-", "" + e);
}
return null;
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
imagecount++;
Log.e("check_count", "" + totalimagecount + "==" + imagecount);
if (totalimagecount == imagecount) {
pDialog.dismiss();
imagecount = 0;
}
Log.e("ffgnjkhjdh", "checkvalue checkvalue" + checkvalue);


}


}

关于android - 如何下载内部存储中特定文件夹中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54510855/

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