0

When I save a bitmap with this function, I get an unreadable file, bigger than the original image(using Root explorer on my phone) what is wrong? The bitmap is set by the user using the stock image picker.

Here is the call:

Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
saveToInternalSorage(bitmap);

Here the saveToInternalSorage method (from here Saving and Reading Bitmaps/Images from Internal memory in Android )

 private String saveToInternalSorage(Bitmap bitmapImage){

    ContextWrapper cw = new ContextWrapper(getApplicationContext());
    // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    String filename = randomString();
    System.out.println(filename);
    File mypath=new File(directory,filename);
    FileOutputStream fos = null;

    try {
        fos = new FileOutputStream(mypath);
        // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return directory.getAbsolutePath();
}
Community
  • 1
  • 1
Gokh
  • 125
  • 1
  • 1
  • 8
  • What is randomString() returning ? – Anoop M Maddasseri Jul 23 '15 at 12:07
  • @AnoopM it returns a String with some random char for an unique image name – Gokh Jul 23 '15 at 12:12
  • Give me an example file name it returns.. – Anoop M Maddasseri Jul 23 '15 at 12:13
  • @AnoopM here the code and one example : psE@B.h;}7 public static String randomString() { Random generator = new Random(); StringBuilder randomStringBuilder = new StringBuilder(); char tempChar; for (int i = 0; i < 10; i++){ tempChar = (char) (generator.nextInt(96) + 32); randomStringBuilder.append(tempChar); } return randomStringBuilder.toString(); } – Gokh Jul 23 '15 at 12:16
  • What is the image name showing in storage after saving ? – Anoop M Maddasseri Jul 23 '15 at 12:19
  • @AnoopM i assume that the image dont need to have the extension or the system to know that its an image, no? – Gokh Jul 23 '15 at 12:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84077/discussion-between-gokh-and-anoop-m). – Gokh Jul 23 '15 at 12:22

0 Answers0