1

After searching about "How to save Layout views as images", I've found some solution to save in Internal and External Storage. But It seems the image file created is going to save in some data/data/... folder that is not visible normally. Actually I want the image visible in gallery for the user. I've found some code like below, but I even can't check if the image is created or not:

View content = findViewById(R.id.relativeLayout);
    String yourimagename = "MyImageFile";
    content.setDrawingCacheEnabled(true);
    Bitmap bitmap = content.getDrawingCache();
    File file = new File("/" + yourimagename + ".png");
    try {
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream ostream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 10, ostream);
        ostream.close();
        content.invalidate();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        content.setDrawingCacheEnabled(false);
    }
Mariox
  • 135
  • 2
  • 11

1 Answers1

1

But It seems the image file created is going to save in some data/data/... folder that is not visible normally.

The file will be saved where the programmer elects to save it.

Actually I want the image visible in gallery for the user. I've found some code like below, but I even can't check if the image is created or not

That code will not work on any version of Android, as new File("/" + yourimagename + ".png") is not going to give you a usable File, as it points to a place that you can neither read nor write.

You are welcome to save the image to internal storage or external storage. Since you want this image to be picked up by "gallery"-type apps, you are best off choosing external storage, then using MediaScannerConnection and its scanFile() method to get the file indexed by the MediaStore, since gallery apps will tend to use the MediaStore as their source of images.

On the whole, I worry that getDrawingCache() will be unreliable. You may be better served telling your root View to draw to your own Bitmap-backed Canvas instead.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491