Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Bitmap to SD Card

I have a Bitmap image which I have to store in a folder in the SD Card, my code is shown below. It creates the folder and file as expected, but the image is not stored into the file, it remains an empty file... Can anyone tell me what's wrong?

Bitmap merged = Bitmap.createBitmap(mDragLayer.getChildAt(0).getWidth(), mDragLayer.getChildAt(0).getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(merged);

// save to folder in sd card
try {
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "folder");
    if(!imagesFolder.exists())
        imagesFolder.mkdirs();
    int imageNum;
    if(imagesFolder.list()==null)
        imageNum = 1;
    else
        imageNum = imagesFolder.list().length + 1;

    String fileName = "file_" + String.valueOf(imageNum) + ".jpg";
    File output = new File(imagesFolder, fileName);
    while(output.exists()){
        imageNum++;
        fileName = "file_" + String.valueOf(imageNum) + ".jpg";
        output = new File(imagesFolder, fileName);
    }

    OutputStream fOut = new FileOutputStream(output);
    merged.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
    fOut.flush();
    fOut.close();

    Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();

   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
}
like image 261
jpmastermind Avatar asked Nov 27 '22 07:11

jpmastermind


2 Answers

First add permission to AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

Then write down in Java File as below.

    String extr = Environment.getExternalStorageDirectory().toString();
            File mFolder = new File(extr + "/MyApp");

            if (!mFolder.exists()) {
                mFolder.mkdir();
            }

            String strF = mFolder.getAbsolutePath();
            File mSubFolder = new File(strF + "/MyApp-SubFolder");

            if (!mSubFolder.exists()) {
                mSubFolder.mkdir();
            }

            String s = "myfile.png";

            f = new File(mSubFolder.getAbsolutePath(),s);

UPDATED

           String strMyImagePath = f.getAbsolutePath();
             FileOutputStream fos = null;
             try {
                 fos = new FileOutputStream(f);
                 bitmap.compress(Bitmap.CompressFormat.PNG,70, fos);

                 fos.flush();
                 fos.close();
              //   MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
             }catch (FileNotFoundException e) {

                 e.printStackTrace();
             } catch (Exception e) {

                 e.printStackTrace();
             }
like image 150
Dhruvil Patel Avatar answered Jan 06 '23 10:01

Dhruvil Patel


Don't make it difficult with complex code its really very simple please Try below code.

Create first dir in your sd card :

public static String strpath =  android.os.Environment.getExternalStorageDirectory().toString();
public static String dirName = "DIR_NAME";

File makeDirectory = new File(strpath+"/"+dirName);
makeDirectory.mkdir();

Then you should make two String Var like below :

String filename =  "yourImageName".jpg";
String dirpath =strpath + "/"+dirName + "/";

Make File Variable :

File storagePath =  new File(dirpath);
File myImage = new File(storagePath, filename);
outStream = new FileOutputStream(myImage);  
outStream.write(data);
outStream.close();

Hope this may helpful to you.

like image 25
Himanshu Dhakecha Avatar answered Jan 06 '23 09:01

Himanshu Dhakecha