0

I need to save a Drawable to a File, and then load if required. The problem is that the Bitmap generated from the functions becomes a black picture once saved. Can anyone help me with that ? I already looked at several similar questions, but none of the answers provided were linked to my issue.

Conversion and File Functions:

public static Bitmap drawableToBitmap(Drawable d) {return Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);}

public static boolean saveDrawableToFile(File dir, String fileName, Drawable d, Bitmap.CompressFormat format, int quality) {
    return saveBitmapToFile(dir, fileName, drawableToBitmap(d), format, quality);
}

public static boolean saveBitmapToFile(File dir, String fileName, Bitmap bm, Bitmap.CompressFormat format, int quality) {

    File imageFile = new File(dir,fileName);

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(imageFile);
        bm.compress(format,quality,fos);
        fos.close();
        return true;
    }
    catch (IOException e) {
        Log.e("FileSaver",e.getMessage());
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    return false;
}

Code in Activity :

File file = new File(getActivity().getCacheDir(),picture.getSamplePath());


        if(file.exists()){
            //Loads local file
            Log.d(LOG_TAG, "Loading thumbnail " + file.getAbsolutePath());
            image = Drawable.createFromPath(file.getAbsolutePath());
        }else{
            //Loads url
            Log.d(LOG_TAG, "Downloading thumbnail " + urls[0]);
            image = QBooruUtils.loadDrawableFromUrl(urls[0]);

            //Saves drawable
            Log.d(LOG_TAG, "Saving thumbnail " + file.getAbsolutePath());
            QBooruUtils.saveDrawableToFile(getActivity().getCacheDir(),picture.getSamplePath(),image, Bitmap.CompressFormat.JPEG,100);
        }

loadDrawableFromUrl is just a function aimed to download the drawable from a remote picture, and it correctly returns a valid Drawable.

Log :

03-06 21:38:04.908 9418-9751/fr.fusoft.qbooru D/PicViewerFrag: Downloading thumbnail http://konachan.com/sample/6e27694cff3910adb4e2e0f1f0fe96dc/Konachan.com%20-%20216418%20sample.jpg
03-06 21:38:05.072 9418-9464/fr.fusoft.qbooru D/OpenGLRenderer: endAllStagingAnimators on 0xb7563a60 (GridView) with handle 0xb767d510
03-06 21:38:05.083 9418-9418/fr.fusoft.qbooru I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3ab4504c time:340520855
03-06 21:38:06.147 9418-9751/fr.fusoft.qbooru D/PicViewerFrag: Saving thumbnail /data/data/fr.fusoft.qbooru/cache/Konachan_216418.jpg
03-06 21:38:08.900 9418-9868/fr.fusoft.qbooru D/PicViewerFrag: Loading thumbnail /data/data/fr.fusoft.qbooru/cache/Konachan_216418.jpg
Elcan
  • 814
  • 13
  • 27

1 Answers1

1

Your function drawableToBitmap() isn't actually drawing the drawable into a bitmap. It's just creating a blank bitmap with the same intrinsic size as the drawable.

You'll need to fill that in with some code that actually converts the drawable to a bitmap.

Community
  • 1
  • 1
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Oh, yes, I forgot to fill it with using a Canvas. I completed it using http://stackoverflow.com/a/10600736/6026551 . Thanks for your help, it works fine now ! – Elcan Mar 06 '16 at 22:20