Search code examples
androidfile-not-foundsave-image

android save picture in internal storage > What am I doing wrong(file not found)


since 6 hours now im trying to simply save a picture into the internal storage of my device(in a new folder). I already read trough a lot of solutions but for some reason im not able to get it to work(File not found).. If you need more code just tell me, but this should be everything relevant to the problem

//OnActivityResult
if (requestCode == CAMERA_TAKE_PICTURE && resultCode == RESULT_OK) {

         
            picureView.setImageURI(lastMedia);
          

        }


//TakePictureIntent
private void cameraTakePictureIntent() {

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        lastMedia = Uri.fromFile(getOutPutMediaFile());
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,lastMedia);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, CAMERA_TAKE_PICTURE);
        }
        Log.d("cameraTakePictureIntent", "Kamera geöffnet");
    }
    
    
//Output media file
private File getOutPutMediaFile(){
        String fileName ="ANFRAGE_"+auftragsNummer+"_"+getAmout(auftragsNummer)+".jpg";
        File mediaStorageDir = new File(getFilesDir().getAbsolutePath()+File.separator+"unsent"+File.separator);
        if(!mediaStorageDir.exists())
            mediaStorageDir.mkdirs();

        return new File(getFilesDir().getAbsolutePath()+File.separator+"unsent"+File.separator+fileName) ;

    }


Solution

  • Please try this code. It work for me.

    // Activity request codes
        private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
        public static final int MEDIA_TYPE_IMAGE = 1;
    
    // directory name to store captured images and videos
        private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";
    
        private Uri fileUri; // file url to store image/video
    

    This is for take image by using camera.

        /*
         * Capturing Camera Image will lauch camera app requrest image capture
         */
        private void captureImage() {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
            fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    
            // start the image capture Intent
            startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
        }
    

    This you getOutputMediaFileUri() method.

        /*
         * Creating file uri to store image
         */
        public Uri getOutputMediaFileUri(int type) {
            return Uri.fromFile(getOutputMediaFile(type));
        }
    

    This you getOutputMediaFile() method.

        /*
         * returning image
         */
        private static File getOutputMediaFile(int type) {
    
            // External sdcard location
            File mediaStorageDir = new File(
                    Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    IMAGE_DIRECTORY_NAME);
    
            // Create the storage directory if it does not exist
            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                            + IMAGE_DIRECTORY_NAME + " directory");
                    return null;
                }
            }
    
            // Create a media file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                    Locale.getDefault()).format(new Date());
            File mediaFile;
            if (type == MEDIA_TYPE_IMAGE) {
                mediaFile = new File(mediaStorageDir.getPath() + File.separator
                        + "IMG_" + timeStamp + ".jpg");
            } else {
                return null;
            }
    
            return mediaFile;
        }
    

    For detail check this tutorial.