0

I am making an android apps that can take photos. I have written some codes to save the photos taken by the app. The directory that I want to save the file is in the internal storage and in the folder named DCIM. However, the app crashes everytime I tried to save the photo. Below is my code:

FileOutputStream outStream = null;
                     try {
                         outStream = new FileOutputStream(String.format(
                                 "sdcard/DCIM/jgjk.jpg", System.currentTimeMillis()));
                         outStream.write(data);
                         outStream.close();
                         Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
                     } catch (FileNotFoundException e) {
                         e.printStackTrace();
                     } catch (IOException e) {
                         e.printStackTrace();
                     } finally {
                     }

Is there anything wrong with the code?

Mary June
  • 139
  • 4
  • 19

1 Answers1

0

This will work fine

Getting Bitmap in onActivityResult

public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
         if(requestCode==PIC_CROP)
        {
             if(resultCode==RESULT_OK)
             {
             Bundle extras = data.getExtras();
                if (extras != null) {               
                    Bitmap bmp = extras.getParcelable("data");
                    savePhoto(bmp);
                }
             }
        }

}


public void savePhoto(Bitmap bmp)
        {
        dir = Environment.getExternalStorageDirectory().toString() + "/DCIM/"; 
        File newdir = new File(dir); 
        newdir.mkdirs();
        FileOutputStream out = null;

        //file name
        Calendar c = Calendar.getInstance();
        String date = fromInt(c.get(Calendar.MONTH))
                    + fromInt(c.get(Calendar.DAY_OF_MONTH))
                    + fromInt(c.get(Calendar.YEAR))
                    + fromInt(c.get(Calendar.HOUR_OF_DAY))
                    + fromInt(c.get(Calendar.MINUTE))
                    + fromInt(c.get(Calendar.SECOND));
        File imageFileName = new File(newdir, "crop_"+date.toString() + ".jpg");

        try
        {
         out = new FileOutputStream(imageFileName);
         bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
         out.flush();
         out.close();
         out = null;
        } catch (Exception e)
        {
        e.printStackTrace();
        }
        }


public String fromInt(int val)
{
return String.valueOf(val);
}
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37