-4

I was wondering what the most efficient way of saving an image on Android is.

What my app basically does is this: you see a default credit card, next you can choose to scan a barcode (with zxing), which will then generate a QR-code for it, and place the QR-code in the ImageView where the default card was stored.

So far so good; however, there's an issue with the app remembering which image to show when navigating away from the result (another screen in the app, going to the homescreen of your device, etc) and going back to the screen. It shows the default card again.

Now, I understand I have to save the QR-code but I can't come up with a solution. The device has no SD-card so saving to external storage is not an option.

I've tried solving it via SharedPreferences, OutputStream, and the cache; but couldn't get it to work.

Which of those 3 ways (or maybe a different one, all help is welcome) would you choose and what would the code be for that?

  • try to save data into sqlite. – Amit Apr 19 '13 at 08:30
  • 1
    http://stackoverflow.com/questions/9942560/when-to-clear-the-cache-dir-in-android – Carnal Apr 19 '13 at 08:32
  • begin with reading the accepted answer, it's about different ways to store data! – Carnal Apr 19 '13 at 08:32
  • http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html – Raghunandan Apr 19 '13 at 08:35
  • what's wrong with internal storage ? – njzk2 Apr 19 '13 at 08:35
  • Thanks so far. Nothing's wrong with internal storage @njzk2. As for the links, they do a great explanation of what to use. I now know I won't mess with cache again, I'll use Files instead. But isn't there a working demo on the Internet somewhere of someone saving a bitmap as a file (that actually works)? – user2298359 Apr 19 '13 at 08:44
  • I posted you saving/reading bitmap, I'm in a good mood today :) – Carnal Apr 19 '13 at 08:52
  • Next time, try something for yourself and ask for help when u are stuck! – Carnal Apr 19 '13 at 08:52
  • Hey @Carnal, thank you very much for your code; but the image still doesn't get saved. I called the getBitmap function in onCreate but that still doesn't work? :/ I also noticed you didn't put fos.write(string.getBitmap()); before fos.close(); or is that irrelevant? – user2298359 Apr 19 '13 at 09:38
  • Why did you close this? I'm being perfectly clear?? – user2298359 Apr 19 '13 at 11:30
  • is irrelevant, when you call compress it will write it, because you also pass the "fos" to the compress method. – Carnal Apr 19 '13 at 12:08
  • Ok, thx, but how come it still doesn't get saved? – user2298359 Apr 19 '13 at 12:52

1 Answers1

1
public boolean saveBitmap(Bitmap image, String name){
    try {
        FileOutputStream fos = context.openFileOutput(name, Context.MODE_PRIVATE);
        image.compress(Bitmap.CompressFormat.JPEG, 70, fos);
        fos.close();
        return true;
    }
    catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

public Bitmap getBitmap(String name){
    try {
        FileInputStream fis = context.openFileInput(name);
        Bitmap image = BitmapFactory.decodeStream(fis);
        return image;
    }
    catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Carnal
  • 21,744
  • 6
  • 60
  • 75