java - 无法下载照片
问题描述
我写了一个从 url 下载照片的程序……但是我有一个问题……下载了一些照片。而且没有问题。
但是有些图片不能完全下载:(而且在文件管理器中我看到它坏了你能帮忙吗?
我的代码是:
公共类 DownloadFileFromURL_img 扩展 AsyncTask {
private viewHolderPost holderPOST;
public DownloadFileFromURL_img(viewHolderPost holderPOST) {
Log.d(TAG, "DownloadFileFromURL_img: ");
this.holderPOST = holderPOST;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* Downloading file in background thread
*/
@Override
protected String doInBackground(String... f_url) {
int count;
try {
File file = new File(Environment.getExternalStorageDirectory(), "98Diha/img");
if (!file.exists()) {
if (!file.mkdirs()) {
file.createNewFile();
}
}
InputStream input = null;
int response = -1;
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
if (!(conection instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conection;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
input = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
int lenghtOfFile = conection.getContentLength();
input = new BufferedInputStream(url.openStream());
String imgS[] = f_url[0].split("/");
String name = imgS[imgS.length - 1];
String path = Environment
.getExternalStorageDirectory().toString()
+ "/98diha/img/" + name;
File filePath = new File(path);
if (!filePath.exists()) {
OutputStream output = new FileOutputStream(path);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} else {
SSToast(context, "Exist!");
holderPOST.dowload_img.setVisibility(View.GONE);
holderPOST.setWallpaper.setVisibility(View.VISIBLE);
holderPOST.setWallpaper.setText(context.getString(R.string.set_wp));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
holderPOST.setWallpaper.setTextColor(ContextCompat.getColor(context, R.color.Teal_400));
} else {
holderPOST.setWallpaper.setTextColor(context.getResources().getColor(R.color.Teal_400));
}
}
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
*/
protected void onProgressUpdate(String... progress) {
holderPOST.dowload_img.setVisibility(View.GONE);
holderPOST.setWallpaper.setVisibility(View.VISIBLE);
holderPOST.setWallpaper.setText(context.getString(R.string.dowloading));
}
/**
* After completing background task Dismiss the progress dialog
**/
@Override
protected void onPostExecute(String file_url) {
holderPOST.setWallpaper.setText(context.getString(R.string.set_wp));
holderPOST.setWallpaper.setTextColor(context.getResources().getColor(R.color.Teal_400));
Log.d(TAG, "onPostExecute: ");
}
}
解决方案
而不是使用AsyncTask
从 URL 下载图像。您可以使用Glide或Picasso之类的库,只需一行即可快速完成。但是,如果您不想使用库,则可以使用DownloadManager下载它并将其保存在文件中。您可以查看本教程或网络上的其他教程以了解 DownloadManager 的实现。
推荐阅读
- deep-learning - 在笔记本电脑上执行庞大的网络进行预测
- bash - 无法使用 crontab 运行脚本
- javascript - 向服务器发送信息时遇到问题
- c - 为什么用 C 语言编写的无操作程序即使在 GCC 中使用 -nostdlib 选项编译时也会占用 14KB?
- c# - 如何在 Microsoft 团队选项卡中获取 url 参数值
- javascript - 获取里面的文字
标记到
在 PHP 中,这是我设法从计算中得到的输出
<p id="generated">12345678199824,12345678411140,12345678921494,12345678497535,</p>
我需要这个值如下
<table> <tr> <td>12345
- javascript - 在构建 Vue.js 项目时使用 Gridsome 时出现 TypeError
- flutter - 如何在颤动中水平扩展导航抽屉内的折叠项目
- google-apps-script - Apps 脚本不使用 MailApp.sendEmail() 发送电子邮件
- javascript - 如何使用winston 和simple() 获得自定义输出?