Save image in current view to sdcard using Android universal-image-loader

大兔子大兔子 提交于 2019-12-12 02:16:55

问题


I'm a newbie Android developer. I have loaded an image using universal-image-loader and I would like to save it on my sd card. The file is created in the desired directory with the correct filename, but it always has a size of 0. What am I doing wrong?

A relevant snippet follows:

PS: The image already exists on disk, it's not being downloaded from the Internet.

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);
    }

回答1:


Yes, your code creates an file on sdcard_root/testdir/de only, and didn't write anything to it. Is "imageUrls2" the source image file? If yes, you can open that file with BufferedInputStream, read the data from BufferedInputStream, and copy them to output file with bos.write() before bos.flush() and bos.close().

Hope it helps.



来源:https://stackoverflow.com/questions/22716418/save-image-in-current-view-to-sdcard-using-android-universal-image-loader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!