Question

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);
    }
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top