【发布时间】:2016-06-23 14:37:27
【问题描述】:
我正在尝试下载图像并将其保存到 app 文件夹,如下所示,一切正常。
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Length of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
storagepath = new File(Environment.getExternalStorageDirectory()
+ "/DownloadedImages");
if (!storagepath.exists()) {
storagepath.mkdirs();
}
OutputStream output = new FileOutputStream(storagepath + "/" + filewithoutextension + ".bmp");
Intent intent =
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(storagepath));
sendBroadcast(intent);
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();
Savetogallery(storagepath);
} catch (Exception e) {}
return null;
}
现在我的问题是下载的图像,当我使用意图打开时,特别是文件夹中没有显示在画廊中,如下所示:
btnOpenpath.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT <= 19) {
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(i, 10);
} else if (Build.VERSION.SDK_INT > 19) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 10);
}
}
});
如果我使用如下所示的下载管理器,那么它会显示文件夹名称。
public void downloadFile(String uRl) {
File direct = new File(Environment.getExternalStorageDirectory()
+ "/SampleFolder");
if (!direct.exists()) {
direct.mkdirs();
}
DownloadManager mgr = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(uRl);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle("Demo")
.setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir("/SampleFolder", "fileName.jpg");
mgr.enqueue(request);
}
【问题讨论】:
-
@downvoter-想发表评论吗?您是否了解我的问题或您不了解的内容,请告诉我将更新我的问题
-
嗨。您的错误是画廊屏幕不显示您存储在文件夹中的下载图像?
-
@GiapLee- 是的..那么我如何使用图库屏幕查看下载的图像
-
是的,我明白了,所以我为你投了赞成票。等我支持。
标签: android url download gallery android-download-manager