Gulmina Khan Khattak
Gulmina Khan Khattak

Reputation: 51

Directory not created while using Camera intent Android

Im trying to save the image taken by teh Camera intent. My code wasnt working so when i added the check whether the directory is even made, it should that it wasnt.What can possibly be wrong? Im trying on the emulator.

01-11 13:50:28.946: D/file(1161):  file is /mnt/sdcard/M3
01-11 13:50:28.946: D/file(1161):  photo is /mnt/sdcard/M3/3_1.jpg

I get the above in the log.

Below is the code i have on the button which opens camera

File sdCard = Environment.getExternalStorageDirectory();
            File file = new File (sdCard.getAbsolutePath() , File.separator + "/M3");
            file.mkdirs();
            String name = e_id + "_" + (size+1)+ ".jpg";
            File newfile = new File(file,name);
            newfile.mkdir();

                Log.d("file"," file is " + file.toString());
                Log.d("file"," photo is " + newfile.toString());


            if(!file.mkdir())
            {
                Log.d("file"," not created ");
                }
            if(!newfile.mkdir())
            {
                Log.d("newfile"," not created ");
                }
            else
            {
                outputFileUri = Uri.fromFile(newfile);
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                startActivityForResult(intent, TAKE_PICTURE);


            }

Upvotes: 0

Views: 143

Answers (2)

trojanfoe
trojanfoe

Reputation: 122381

The issue is that you are treating the image file as a directory:

newfile.mkdir();

Try:

File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath(), File.separator + "/M3");
if (!dir.mkdirs())
{
    Log.e(TAG, "Failed to create directory " + dir);
    return false;    // Assuming this is in a method returning boolean
}
String filename = e_id + "_" + (size+1)+ ".jpg";
File file = new File(dir, filename);

Log.d(TAG, "dir is " + dir.toString());
Log.d(TAF, "file is " + file.toString());

outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);

Note: you need to name your variables better; file isn't a file it's a directory. newfile is the only file you care about in this code snippet, so call it file.

Upvotes: 1

Nirav Tukadiya
Nirav Tukadiya

Reputation: 3417

replace this line

File file = new File (sdCard.getAbsolutePath() , File.separator + "/M3");

with this one.

File file = new File (sdCard.getAbsolutePath()+"/YourDirectoryName");

Upvotes: 0

Related Questions