如何将图像保存在设备中

时间:2017-12-26 14:49:53

标签: java android imageview android-glide

如何在使用Glide下载图像后将图像保存在设备中。

 Glide.with(Main2Activity.this)
            .load(string)
            .into(imageView);

3 个答案:

答案 0 :(得分:0)

当你做

Glide.with(this)
           .load(string)
           .into(imageView);

图像会自动缓存,如果您的应用中的内容没有发生变化,那么它会从手机的缓存中加载相同的缓存图像。

如果你想下载到你的画廊,那就完全不同了

https://stackoverflow.com/a/15549639/6683139

答案 1 :(得分:0)

这是答案

 public void downloadFile(String url) {
    File direct = new File(Environment.getExternalStorageDirectory()
            + "/AnhsirkDasarp");

    if (!direct.exists()) {
        direct.mkdirs();
    }

    DownloadManager mgr = (DownloadManager) getApplicationContext().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("/AnhsirkDasarpFiles", "fileName.jpg");

    mgr.enqueue(request);

}

............................................... .................................................. .................................................. .................................................. .................................................. ...........

答案 2 :(得分:0)

试试这个:

上面的

Glide 4.0使用此代码:

    Glide.with(this)
            .asBitmap()
            .load(path)
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
                    imageView.setImageBitmap(bitmap);

                    //Bitmap storing to external storage
                    try {
                        if (bitmap != null) {
                            File newfile = new File(Environment.getExternalStorageDirectory() + "/directoryname");
                            if (!newfile.isDirectory()) {
                                newfile.mkdir();
                            }
                            newfile = new File(Environment.getExternalStorageDirectory() + "/directoryname", "_" + System.currentTimeMillis() + ".jpg");
                            try {
                                FileOutputStream fileOutputStream = new FileOutputStream(newfile);
                                if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream)) {
                                    //Image saved
                                } else {
                                    //Image saving unsuccessfull
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            } finally {
                                try {
                                    if (fileOutputStream != null) {
                                        fileOutputStream.close();
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

获取上述marsmallow的WRITE_EXTERNAL_STORAGE手动请求权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>