Android, Saving and loading a bitmap in cache from different activities

I have i bitmap that needs to show in a new activity, so i cahe it and in the opened activity i try to load it but i get a nullPointerException. Here i save the image :

File cacheDir = getBaseContext().getCacheDir();
File f = new File(cacheDir, "pic");

try {
    FileOutputStream out = new FileOutputStream(
            f);
    pic.compress(
            Bitmap.CompressFormat.JPEG,
            100, out);
    out.flush();
    out.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Intent intent = new Intent(
        AndroidActivity.this,
        OpenPictureActivity.class);
startActivity(intent);

and then in the new activity i try to open it :

  • ViewSwitcher and layout height in Android
  • Android: call custom methods in a ContentProvider
  • Remove ListView footer Click Listener
  • getWidth Returns 0 in Fragment, getPaddingLeft Returns Non-Zero
  • Android Developer Dashboard national/regional version
  • NullPointerException on findViewById() in android
  • public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
    
        File cacheDir = getBaseContext().getCacheDir();
        File f = new File(cacheDir, "pic");     
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(f);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(fis);
    
        ImageView viewBitmap = (ImageView) findViewById(R.id.icon2);
        viewBitmap.setImageBitmap(bitmap);
        setContentView(R.layout.open_pic_layout);
    

    Related posts:

    LogCat message: The Google Play services resources were not found. Check your project configuration ...
    Class not found: Empty test suite when running unit tests in Android Studio
    Why is App not compatible with Tablets after update
    Navigation Drawer Android API specific Layout Attribute
    Using FLAG_ACTIVITY_REORDER_TO_FRONT to switch among persistently running UI activities leads to &am...
    Dynamic view with swiping horizontally and vertically
  • How can I verify image URI is valid in Android?
  • Spinner malfunctioning on Lollipop
  • IndexOutOfBoundsException after repopulating ArrayList (Marshmallow only)
  • Ripple effect on custom navigation drawer
  • How do I determine whether a path is a local file or not
  • How do I implement the type of page curl used in Google Play Books?
  • One Solution collect form web for “Android, Saving and loading a bitmap in cache from different activities”

    Just check your code:

    ImageView viewBitmap = (ImageView) findViewById(R.id.icon2);
    viewBitmap.setImageBitmap(bitmap);
    setContentView(R.layout.open_pic_layout);
    

    You have written findViewById() before setting Content View. Its wrong.

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.open_pic_layout);
    
      // do your operations here
      }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.