Search code examples
androidbitmapscreenshot

take a screenshot in android then use it in another activity


So I have seen alot of questions and answers on how to take a screenshot and save it to external storage, but what I would like to know is how after I have done this I can read it into the next activity,
I will also be using the image when I open the app another time so I don't think a bundle will be enough.
I know it seems simple and probably is so thanks in advance.


Solution

  • You can use intent bundle to pass the screenshot's file path between Activities, and you can use SharedPreferences to persistent the path for next time loading.

    Save bitmap to external storage:

    FileOutputStream fos = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();
    String path = file.getPath();
    

    Pass path between Activities:

    intent.putExtra(PATH, path);
    

    Persistence:

    SharedPreferences sp = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
    sp.edit().putString(PATH, path).commit(); //save
    sp.getString(PATH, null); //read