Far*_*emi 16 android download picasso
我试图在自定义ListView中显示我的新闻.每个新闻都包括一些图像,我想
1. 从服务器下载图像
2.在本地存储中保存
3.save 路径的图像成的SQLite
4. 使用我的自定义适配器在ListView中显示图像.
我刚刚遇到第1步和第2步的问题.我可以从服务器获取新闻并在我的ListView中显示它们
并通过在我的适配器中添加以下代码来显示缓存中的图像:
Picasso.with(context).load(image[position]).into(iv);
Run Code Online (Sandbox Code Playgroud)
通过使用Picasso.with(context).load(image[position]).into(target),我可以保存一个
存储中的图像.
请建议我你的想法......
更新:当我使用下面的代码时,只保存一个图像(我的图像数组的最后一个索引)!
如何使用此代码保存数组中的所有图像?!
@Override
protected void onPostExecute(Void result) {
SaveImages();
pDialog.dismiss();
super.onPostExecute(result);
}
String fileName = null;
public void SaveImages() {
for(int i = 0; i < image.length; i++) {
Picasso.with(this).load(image[i]).into(target);
fileName = "image-" + i + ".jpg";
}
}
Target target = new Target() {
@Override
public void onPrepareLoad(Drawable arg0) {
}
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
File file = new File(Environment.getExternalStorageDirectory().getPath() +"/" + fileName);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 75, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onBitmapFailed(Drawable arg0) {
}
};
Run Code Online (Sandbox Code Playgroud)
尝试将"目标目标"定义放在"Picasso.with(this).load(image [i]).into(target);"之前.
PS我在这里使用此代码并很好地保存图像.不管怎么说,还是要谢谢你.
我的代码:
final String fileName = mDataset.get(i).getAid() + ".jpg";
Target target = new Target() {
@Override
public void onPrepareLoad(Drawable arg0) {
return;
}
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom arg1) {
try {
File file = null;
// judge "imgs/.nomedia"'s existance to judge whether path available
if(LightCache.testFileExist(GlobalConfig.getFirstStoragePath()
+ "imgs" + File.separator +".nomedia") == true)
file = new File(GlobalConfig.getFirstStoragePath()
+ "imgs" + File.separator + fileName);
else file = new File(GlobalConfig.getSecondStoragePath()
+ "imgs" + File.separator + fileName);
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onBitmapFailed(Drawable arg0) {
return;
}
};
Picasso.with(GlobalConfig.getContext())
.load(Wenku8API.getCoverURL(mDataset.get(i).getAid()))
.into(target);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27091 次 |
| 最近记录: |