我正在尝试下载图片并将其保存到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);
}
答案 0 :(得分:2)