Android下载并在后台保存大量图像
在我的Android应用程序中,我想下载一定数量的照片,这些照片链接到地图上的点。例如,我在地图上有10个点,每个点有5到6张照片 在开始的时候,我每点得到5或6个URL(),对于每张照片Android下载并在后台保存大量图像,android,android-asynctask,download,universal-image-loader,Android,Android Asynctask,Download,Universal Image Loader,在我的Android应用程序中,我想下载一定数量的照片,这些照片链接到地图上的点。例如,我在地图上有10个点,每个点有5到6张照片 在开始的时候,我每点得到5或6个URL(),对于每张照片 我想下载它们 我想把它们保存在设备中 当然,一切都是在后台完成的。 问题是,有时我的图像不会持久化。。。但是这个问题是非常不规则的,在任何图像上都会遇到 这是我的密码: 对于每个点,loadPictures()被调用: private void loadPictures(final KickstartrG
- 我想下载它们
- 我想把它们保存在设备中
private void loadPictures(final KickstartrGeolocPoint point) {
//Photos : get directory path
final File pointDir = FileUtils.getPointPhotoDir(getActivity(), point);
//set boolean to true if the point has photos not downloaded.
ArrayList<String> photosNotDownloaded = getPhotosNotDownloaded(point, pointDir);
if(photosNotDownloaded.size()!=0){
point.setPhoto(true);
kickstartrGeolocService.updatePoint(currentPoint);
}else{
fillPointData(point);
return;
}
//for each urls, download and save photo
for(final String url : photosNotDownloaded){
if (!Utils.isValidUrl(url)) continue;
BitmapUtils.getBitmapFromUrl(url, new OnBitmapRetrievedListener() {
@Override
public void bitmapRetrieved(Bitmap bitmap) {
LogWrapper.debug(getClass(), "Ready to be saved");
persistImageforPoint(point, pointDir, bitmap, FileUtils.getFileNameFromUrl(url));
}
@Override
public void onLoadingStarted(String imageUri, View view) {
LogWrapper.debug(getClass(), "Image downloading... " + imageUri);
//YOU CAN NOTIFY THE VIEW HERE
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
LogWrapper.debug(getClass(), "Loading failed : " + imageUri);
}
});
}
}
调用persistImageforPoint()保存照片:
private void persistImageforPoint(KickstartrGeolocPoint point, File pointDir, Bitmap bitmap, String imageName) {
// Create a subfolder for each point with its id
if (!pointDir.exists()) {
if (!pointDir.mkdirs()) {
LogWrapper.debug(FileUtils.class, "Failed to create directory");
return;
}
}
final String fullPath = pointDir.getPath()+File.separator+FileUtils.getPointPhotoPrefix(point)+FileUtils.getFileNameFromUrl(imageName);
//save the file. Asynchronous task --> do not block the UI
new BitmapPersist(bitmap,fullPath, new OnBitmapPersistedListener() {
@Override
public void persistedSuccessfully() {
addPhotoView(fullPath);
}
@Override
public void errorInPersistance() {
LogWrapper.error(getClass(),"Error persisting image with path "+fullPath);
}
}).execute();
}
这是我的BitmapPersist类:
public class BitmapPersist extends AsyncTask<String, Void, Boolean> {
private String path;
private Bitmap bitmap;
private OnBitmapPersistedListener onBitmapPersistedListener;
public BitmapPersist(Bitmap bitmap, String path, OnBitmapPersistedListener onBitmapPersistedListener) {
this.bitmap=bitmap;
this.path=path;
this.onBitmapPersistedListener=onBitmapPersistedListener;
}
@Override
protected Boolean doInBackground(String... params) {
FileOutputStream out = null;
try {
out = new FileOutputStream(path);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try{
out.close();
} catch(Throwable ignore) {}
}
return false;
}
@Override
protected void onPostExecute(Boolean success) {
if (success)
onBitmapPersistedListener.persistedSuccessfully();
onBitmapPersistedListener.errorInPersistance();
}
}
公共类BitmapPersist扩展异步任务{
私有字符串路径;
私有位图;
私有OnBitmapPersistedListener OnBitmapPersistedListener;
公共位图持久化(位图位图、字符串路径、OnBitmapPersistedListener OnBitmapPersistedListener){
this.bitmap=位图;
this.path=path;
this.onBitmapPersistedListener=onBitmapPersistedListener;
}
@凌驾
受保护的布尔doInBackground(字符串…参数){
FileOutputStream out=null;
试一试{
out=新文件输出流(路径);
bitmap.compress(bitmap.CompressFormat.JPEG,90,out);
返回true;
}捕获(例外e){
e、 printStackTrace();
}最后{
试一试{
out.close();
}捕获(可丢弃忽略){}
}
返回false;
}
@凌驾
受保护的void onPostExecute(布尔值成功){
如果(成功)
onBitmapPersistedListener.persistedSuccessfully();
onBitmapPersistedListener.errorInPersistance();
}
}
问题:有时我的图片未保存,有时应用程序崩溃。。。虽然一切都是在后台线程中完成的
谢谢你的帮助
编辑:也许我可以在用于下载图像的库中添加精确性:它是Android通用图像加载器(上面的链接)。此库使用内存缓存和。。。磁盘缓存。也许我可以看看这个缓存。也许我可以更改磁盘缓存的路径并用它保存我的图片。我也在寻找一个知道这个库的人(由诺斯特拉开发)。。。文档非常简洁
解决方案:不需要创建两个异步任务:同步下载图片,然后将其保存在同一个异步任务中。崩溃的原因是什么?Post logsI现在不能这么做,因为我今天没有做。但我会的,是的!就等几个小时吧……;)好的,我想我已经解决了我的问题。我不需要2个Asynctasks:我只开发了一个asynctask,可以同时下载(同步)和保存。
public class BitmapPersist extends AsyncTask<String, Void, Boolean> {
private String path;
private Bitmap bitmap;
private OnBitmapPersistedListener onBitmapPersistedListener;
public BitmapPersist(Bitmap bitmap, String path, OnBitmapPersistedListener onBitmapPersistedListener) {
this.bitmap=bitmap;
this.path=path;
this.onBitmapPersistedListener=onBitmapPersistedListener;
}
@Override
protected Boolean doInBackground(String... params) {
FileOutputStream out = null;
try {
out = new FileOutputStream(path);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try{
out.close();
} catch(Throwable ignore) {}
}
return false;
}
@Override
protected void onPostExecute(Boolean success) {
if (success)
onBitmapPersistedListener.persistedSuccessfully();
onBitmapPersistedListener.errorInPersistance();
}
}