0

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) ;

    }
TBrauwers
  • 21
  • 1
  • 8
  • What is error log? – Farmer Mar 06 '17 at 14:38
  • Unable to open content: file:///data/user/0/de.comidos.fotoapp/files/unsent/ANFRAGE_123456_1.jpg java.io.FileNotFoundException: /data/user/0/de.comidos.fotoapp/files/unsent/ANFRAGE_123456_1.jpg (No such file or directory) – TBrauwers Mar 06 '17 at 14:39
  • You try to open file that is not exist in your device.Please make sure that your file is exist or not. – Farmer Mar 06 '17 at 14:45
  • I know, thats my problem... The file should get created with the intent but its not. – TBrauwers Mar 06 '17 at 14:50
  • Probably you can not create directory when you start intent. If you want to save file in a specific location then first take image and then after do save code in `onActivityResult()` method – Farmer Mar 06 '17 at 15:02
  • The directories are created before the intent. Ill look into this but still "my" way should work because I took it from various "tutorials" on stack and other sides and just changed the path – TBrauwers Mar 06 '17 at 15:08
  • `mediaStorageDir.mkdirs();`. Dont believe that it does. Check the return value. Change to if(!mediaStorageDir.mkdirs()){ toast (could not mkdir mediaStorageDir.getAbsolutePath()); return null;} – greenapps Mar 06 '17 at 15:24
  • if(!mediaStorageDir.exists()) if(! mediaStorageDir.mkdirs()) { Log.d("Dir Error", mediaStorageDir.getAbsolutePath()); return null ; } Have it like this now but not getting an error for this – TBrauwers Mar 06 '17 at 15:35

2 Answers2

0

Have you tried:

File file = new File(context.getFilesDir(), filename);

?

Saving Files | Android Developers

inwi
  • 129
  • 1
  • 5
  • I need the image to get stored in the "unsent" folder inside my internal storage – TBrauwers Mar 06 '17 at 14:46
  • Like this? `File mediaStorageDir = new File(context.getFilesDir().getAbsolutePath()+File.separator+"unsent"+File.separator);` – inwi Mar 06 '17 at 14:51
  • Ok, what about `getCacheDir()` or `openFileOutput()`? – inwi Mar 06 '17 at 14:57
  • getCacheDir is not what im looking fore since i want to store images persistently. What do you mean by openFileOutput? – TBrauwers Mar 06 '17 at 15:00
  • I'd try `getCacheDir()` for debugging. Please refer to the linked documentation above for how to use `openFileOutput()`. – inwi Mar 06 '17 at 15:06
  • I cant acess CacheDir like i cant acess the Internal Storage(No rooted device and the emulator doesnt work properly on this pc) so could you please explain a little bit how it could help me with debugging? – TBrauwers Mar 06 '17 at 15:12
0

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.

Farmer
  • 4,093
  • 3
  • 23
  • 47