1

I have an activity for add new post, in each post user can select picture from gallery or take picture with camera. My save image method is ok for picture from camera but when I choose a picture from gallery, in my folder just save name of picture with a black picture.

public void saveImage(Bitmap myBitmap) {
   ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    File wallpaperDirectory = new File(
            Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);

    if (!wallpaperDirectory.exists()) {
        wallpaperDirectory.mkdirs();
    }

    try {
        File f = new File(wallpaperDirectory, Calendar.getInstance()
                .getTimeInMillis() + ".jpg");
        img_name = f.getAbsolutePath();
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        MediaScannerConnection.scanFile(this,
                new String[]{f.getPath()},
                new String[]{"image/jpeg"}, null);
        fo.close();
        Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());

        return f.getAbsolutePath();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return "";

And in my RecyclerView shows nothing.

George
  • 6,886
  • 3
  • 44
  • 56
Elham
  • 35
  • 1
  • 9
  • refer this link https://stackoverflow.com/questions/8969072/android-save-images-to-specific-folder-in-the-sd-card?answertab=votes#tab-top –  Dec 10 '18 at 09:11
  • may help see here https://stackoverflow.com/questions/7887078/android-saving-file-to-external-storage?noredirect=1&lq=1 – Pankaj Singh Dec 10 '18 at 09:12

1 Answers1

0

You are just creating a new image file and not saving anything in it . use following

FileOutputStream fo = new FileOutputStream(f);
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, fo); // bmp is your Bitmap instance
fo.write(bytes.toByteArray());

you are missing this myBitmap.compress(Bitmap.CompressFormat.PNG, 100, fo); so add it.

Uma Achanta
  • 3,669
  • 4
  • 22
  • 49