【问题标题】:Save image in current view to sdcard using Android universal-image-loader使用 Android 通用图像加载器将当前视图中的图像保存到 sdcard
【发布时间】:2014-05-08 03:02:20
【问题描述】:

我是一名新手 Android 开发人员。我已经使用 Universal-image-loader 加载了一个图像,我想将它保存在我的 SD 卡上。该文件以正确的文件名在所需目录中创建,但其大小始终为 0。我做错了什么?

一个相关的sn-p如下:

PS:图片已经存在磁盘上,不是从网上下载的。

private void saveImage(String imageUrls2, String de) {
        String filepath = Environment.getExternalStorageDirectory()
                .getAbsolutePath();
        File SDCardRoot = Environment.getExternalStorageDirectory()
                .getAbsoluteFile();     
        String filename = de;
        File myDir = new File(SDCardRoot+"/testdir");
        Bitmap mSaveBit = imageLoader.getMemoryCache();
        File imageFile = null;

        try {           
            //create our directory if it does'nt exist
            if (!myDir.exists())
                myDir.mkdirs();

            File file = new File(myDir, filename);
            if (file.exists())
                file.delete();

            FileOutputStream fileOutputStream = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

            bos.flush();
            bos.close();

        } catch (IOException e) {
            filepath = null;
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),
                    R.string.diskful_error_message, Toast.LENGTH_LONG)
                    .show();
        }
        Log.i("filepath:", " " + filepath);
    }

【问题讨论】:

标签: android universal-image-loader


【解决方案1】:

是的,您的代码仅在 sdcard_root/testdir/de 上创建了一个文件,并且没有向其中写入任何内容。 “imageUrls2”是源图像文件吗?如果是,您可以使用 BufferedInputStream 打开该文件,从 BufferedInputStream 读取数据,然后在 bos.flush() 和 bos.close() 之前使用 bos.write() 将它们复制到输出文件。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    相关资源
    最近更新 更多