我在做一个学校的机器人项目。我需要一个下载按钮,它下载图片(当我们有类时),然后在另一个活动中显示它(即使在离线模式下,在退出之后)。
我试过毕加索,但我无法让它在离线模式下保存和使用。
发布于 2015-10-11 13:09:10
要支持脱机模式,需要保存磁盘上的映像,因为当缓存被清除时,映像也会被清除。
您可以很容易地使用Glide解决这个问题,还可以存储在设备上并检索。
您可以在这里了解更多关于幻灯片的信息,http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en
/** Download the image using Glide **/
Bitmap theBitmap = null;
theBitmap = Glide.
with(YourActivity.this).
load("Url of your image").
asBitmap().
into(-1, -1).
get();
saveToInternalStorage(theBitmap, getApplicationContext(), "your preferred image name");
/** Save it on your device **/
public String saveToInternalStorage(Bitmap bitmapImage, Context context, String name){
ContextWrapper cw = new ContextWrapper(context);
// path to /data/data/yourapp/app_data/imageDir
String name_="foldername"; //Folder name in device android/data/
File directory = cw.getDir(name_, Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,name);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("absolutepath ", directory.getAbsolutePath());
return directory.getAbsolutePath();
}
/** Method to retrieve image from your device **/
public Bitmap loadImageFromStorage(String path, String name)
{
Bitmap b;
String name_="foldername";
try {
File f=new File(path, name_);
b = BitmapFactory.decodeStream(new FileInputStream(f));
return b;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return null;
}
/** Retrieve your image from device and set to imageview **/
//Provide your image path and name of the image your previously used.
Bitmap b= loadImageFromStorage(String path, String name)
ImageView img=(ImageView)findViewById(R.id.your_image_id);
img.setImageBitmap(b);发布于 2015-10-11 12:47:08
您可以使用称为通用图像加载器的Android库
发布于 2015-10-11 13:13:50
感谢@Droidman:How to download and save an image in Android
当然,您可以自己执行下载和管理映像,但如果您的项目已经相当复杂,周围有很多库,您不需要重新发明轮子。这一次我不会发布代码,因为这里有很多例子,但我将告诉您两个与图像下载相关的最有用的库(IMO)。 1) Android截击。一个强大的网络库,由谷歌创建,并由官方文档覆盖。发布或获取数据,图片,JSON - volley将为您管理它。在我看来,用截击来下载图片有点过头了。 2)毕加索 图片下载和缓存,完美的ListView/GridView/回收视图。Apache2.0许可证。 ( 3)弗雷斯科 一个相当新的图像加载库由Facebook创建。进步JPEG流,gifs和更多。Apache2.0
https://stackoverflow.com/questions/33065071
复制相似问题