首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >下载图片并保存

下载图片并保存
EN

Stack Overflow用户
提问于 2015-10-11 12:41:21
回答 3查看 4.5K关注 0票数 0

我在做一个学校的机器人项目。我需要一个下载按钮,它下载图片(当我们有类时),然后在另一个活动中显示它(即使在离线模式下,在退出之后)。

我试过毕加索,但我无法让它在离线模式下保存和使用。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-10-11 13:09:10

要支持脱机模式,需要保存磁盘上的映像,因为当缓存被清除时,映像也会被清除。

您可以很容易地使用Glide解决这个问题,还可以存储在设备上并检索。

您可以在这里了解更多关于幻灯片的信息,http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

代码语言:javascript
复制
/** 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);
票数 2
EN

Stack Overflow用户

发布于 2015-10-11 12:47:08

您可以使用称为通用图像加载器的Android库

https://github.com/nostra13/Android-Universal-Image-Loader

票数 0
EN

Stack Overflow用户

发布于 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33065071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档