【问题标题】:How to download and save image into the app directory using Ion library如何使用 Ion 库下载图像并将其保存到应用程序目录
【发布时间】:2016-04-04 06:30:12
【问题描述】:

我正在使用 Ion 库,它运行良好。但另一方面,我必须下载图像并将其保存到 app 目录。

我想要做的是,在导航视图标题中使用图像视图,并希望下载一次图像并在下一次显示该图像,除非我解除它

所以我使用 Ion 库来调用服务器,所以我在想为什么不使用 Ion 库来达到这个目的。我只知道它具有下载图像并在图像视图中显示它的有效功能

 // This is the "long" way to do build an ImageView request... it allows you to set headers, etc.
Ion.with(context)
.load("http://example.com/image.png")
.withBitmap()
.placeholder(R.drawable.placeholder_image)
.error(R.drawable.error_image)
.animateLoad(spinAnimation)
.animateIn(fadeInAnimation)
.intoImageView(imageView);

// but for brevity, use the ImageView specific builder...
Ion.with(imageView)
.placeholder(R.drawable.placeholder_image)
.error(R.drawable.error_image)
.animateLoad(spinAnimation)
.animateIn(fadeInAnimation)
.load("http://example.com/image.png");

我真的不知道如何使用它并将图像保存在应用程序目录中,而不是直接在图像视图中显示它。

我想下载图像并将图像保存在应用程序目录中。我可以为此目的使用 Ion 库还是我必须做更多的事情。 ??请帮忙。

【问题讨论】:

    标签: android imageview android-ion


    【解决方案1】:
    Ion.with(this).load("url").withBitmap().asBitmap()
        .setCallback(new FutureCallback<Bitmap>() {
            @Override
            public void onCompleted(Exception e, Bitmap result) {
                // do something with your bitmap
            }
        });
    

    您将在 onCompleted 回调中获得一个位图,即您的图像作为位图。现在您可以轻松地保存在您想要的位置。

    保存位图:

    //the string here is the file name
            public static void saveBitmap(Context context, Bitmap original, String name) {
        try {
        File imageFile = createPngFileFromBitmap(name, original);
        addImageToGallery(imageFile.getAbsolutePath(), context);
    
    
        } catch (FileNotFoundException e) {
        e.printStackTrace();
        }
    
        }
    
        public static void addImageToGallery(final String filePath, final Context context) {
    
        ContentValues values = new ContentValues();
    
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
        values.put(MediaStore.MediaColumns.DATA, filePath);
    
    
        context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        }
    
        public static File createPngFileFromBitmap(String name, Bitmap bitmap) throws FileNotFoundException {
        File picture = createPngFile(name);
        OutputStream stream = new FileOutputStream(picture);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    
        return picture;
        }
    
        private static File createPngFile(String name) {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Your folder name");
        if (!file.mkdirs()) {
        } else {
        file.mkdirs();
        }
        return new File(file.getAbsolutePath(), name);
        }
    

    祝你好运!

    【讨论】:

    • 你能告诉我如何将这个位图(结果)保存在应用程序目录中。我的意思是在 com.mypacjage.app 文件夹中
    • 但这会保存在外部目录而我想保存在应用程序文件夹中,我的意思是应用程序安装文件夹
    • 我认为你做不到。更好的方法,如果你用你的应用名称创建一个文件夹并存储在那里。
    • 在罗伯特,很明显我们可以做到这一点,让我试试,如果你的代码有效,你会得到你的价格,:)
    • 如您所见,facebook、instagram、snapchat 等将图像保存到具有自己文件夹的图库中。祝你好运,如果你有任何问题.. :)
    【解决方案2】:

    是的,您可以使用 Ion 将图像保存到您的内部/外部存储器。你可以参考他们的官方文档more detail

    Ion.with(context)
    .load("http://example.com/really-big-file.zip")
    // have a ProgressBar get updated automatically with the percent
    .progressBar(progressBar)
    // and a ProgressDialog
    .progressDialog(progressDialog)
    // can also use a custom callback
    .progress(new ProgressCallback() {@Override
       public void onProgress(int downloaded, int total) {
           System.out.println("" + downloaded + " / " + total);
       }
    })
    .write(new File("/sdcard/really-big-file.zip"))
    .setCallback(new FutureCallback<File>() {
       @Override
        public void onCompleted(Exception e, File file) {
            // download done...
            // do stuff with the File or error
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2023-03-04
      • 1970-01-01
      • 2017-04-12
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      相关资源
      最近更新 更多