Search code examples
androidandroid-cameraandroid-galleryandroid-fileandroid-camera-intent

Save Custom name for picture via camera intent in DCIM/pictures


I have followed and literally picked up code from other answers but its still not working.

When a picture is clicked, it's saved by default in DCIM/camera with name such as 20150910_111841.jpg (date_timeInMilliseconds) but I want to save it as "myName_date.jpg".

public void OnClick(){
 if (items[item].equals("Take Photo")) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                    mMediaUri=Uri.fromFile(new File(f, "MEDIT"+System.currentTimeMillis()+"jpg"));
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
                    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode==RESULT_OK){
        switch(requestCode) {
            case REQUEST_IMAGE_CAPTURE:
                Bitmap bm = BitmapFactory.decodeFile(mMediaUri.getPath());
                //imgVIew.setImageBitmap(bm);

                Bitmap scaledBitmap=ScaleImage(bm);
                final ByteArrayOutputStream bos = new ByteArrayOutputStream();
                scaledBitmap.compress(Bitmap.CompressFormat.JPEG,100, bos);
                //Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(bos.toByteArray()));
                imgVIew.setImageBitmap(scaledBitmap);
                imageView.setImage(ImageSource.bitmap(scaledBitmap));
                imageView1.setImage(ImageSource.bitmap(bm));
                    break;
            case REQUEST_GALLERY:

                    //Uri selectedImage = imageReturnedIntent.getData();
                    //imageview.setImageURI(selectedImage);
                    break;
        }//Close switch statement

    }else {
        Log.d("Error ", "user canceled !");
    }

}

I have not yet added code for the gallery part but that shouldn't be a problem. As of now no matter what I do its being saved as default but I realized if people don't have an SD card then it can cause an issue. I'd rather have this picture available, in the default DCIM location but with a custom name. Thanks!


Solution

  • Try the below code to save the image with the name you want:

    /** Create a File for saving an image or video */
        private static File getOutputMediaFile(int type) {
            // To be safe, you should check that the SDCard is mounted
            // using Environment.getExternalStorageState() before doing this.
            /*
            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                      Environment.DIRECTORY_PICTURES), "MyCameraApp");
            */
            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DCIM), "the_name_of_directory");
    
            // This location works best if you want the created images to be shared
            // between applications and persist after your app has been uninstalled.
    
            // Create the storage directory if it does not exist
            if (! mediaStorageDir.exists()){
                if (! mediaStorageDir.mkdirs()){
                    Log.d("canberra trailpass", "failed to create directory");
                    return null;
                }
            }
    
            // Create a media file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            File mediaFile;
            if (type == MEDIA_TYPE_IMAGE){
                mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "myName"+ timeStamp + ".jpg");
            } else {
                return null;
            }
    
            return mediaFile;
        }