android 如何通过URL下载图像到内存?

unguejic  于 2023-01-07  发布在  Android
关注(0)|答案(2)|浏览(106)

我想下载已上传到Firebase的图像。我获得了URL,并能够将它们直接加载到所需的ImageView。但我想将它们下载到内部存储中的所需路径
我使用过Picasso,但它对我不起作用,因为我在使用**(new Target())**时得到了下面所附的错误。这是我使用Picasso和Target时得到的错误

毕加索的密码如下

Picasso.get()
                .load(downloadUrl)
                .into(new Target() {
                          @Override
                          public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                              try {

                                  String root = Environment.getExternalStorageDirectory().toString();
                                  File myDir = new File(root + "/yourDirectory");

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

                                  String name = new Date().toString() + ".jpg";
                                  myDir = new File(myDir, name);
                                  FileOutputStream out = new FileOutputStream(myDir);
                                  bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

                                  out.flush();
                                  out.close();
                              } catch(Exception e){
                                  // some action
                              }
                          }

                          @Override
                          public void onBitmapFailed(Drawable errorDrawable) {

                          //Some Action;
                          }

                          @Override
                          public void onPrepareLoad(Drawable placeHolderDrawable) {
                            //Some Action;
                          }
                      }
                );

我已经在文档中搜索,但没有工作。请让我尽快知道。如果有人有正确的答案。提前感谢

jgzswidk

jgzswidk1#

第一个:实现Taget类方法清 debugging 误
second:您的代码抛出此exception java.io.FileNotFoundException,因为Date对象的默认格式与Mon Jan 21 12:10:23 GMT+02:00 2019类似,并且您无法创建名称包含**:**冒号的文件
**第三:**检查是否在androidManiefest.xml中添加了android.permission.WRITE_EXTERNAL_STORAGE权限,并且需要在marshmallow中使用运行时权限

mnemlml8

mnemlml82#

**注意:**要删 debugging 误,必须实现Target类的方法。

对于下载映像,您可以从链接中找到解决方案
https://www.codexpedia.com/android/android-download-and-save-image-through-picasso/

Picasso.with(this).load(anImageUrl).into(picassoImageTarget(getApplicationContext(), "imageDir", "my_image.jpeg"));

方法

private Target picassoImageTarget(Context context, final String
imageDir, final String imageName) {
    Log.d("picassoImageTarget", " picassoImageTarget");
    ContextWrapper cw = new ContextWrapper(context);
    final File directory = cw.getDir(imageDir, Context.MODE_PRIVATE); // path to /data/data/yourapp/app_imageDir
    return new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final File myImageFile = new File(directory, imageName); // Create image file
                    FileOutputStream fos = null;
                    try {
                        fos = new FileOutputStream(myImageFile);
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                   Log.i("image", "image saved to >>>" + myImageFile.getAbsolutePath());

                }
            }).start();
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
        }
        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            if (placeHolderDrawable != null) {}
        }
    }; }

相关问题