Creating directory hierarchy in Android (for gallery) - android

I have some code like this:
photo = new File(android.os.Environment.getExternalStorageDirectory(), "images/mydir/" + File.separator + timeStamp + ".jpg");
photo.getParentFile().mkdirs();
photo.createNewFile();
But in the gallery, I'm only seeing "mydir" with the photos inside it. No "images" directory to be found.
How do I create a hierarchy so there's an image folder and mydir is inside that?
Thanks

You have problem in your code as you have one / after mydir and you are also adding File.separator so final output will be images/mydir//
photo = new File(android.os.Environment.getExternalStorageDirectory(), "images/mydir/" + File.separator + timeStamp + ".jpg");
Just remove the / after mydir like below
photo = new File(android.os.Environment.getExternalStorageDirectory(), "/images/mydir" + File.separator + timeStamp + ".jpg");
This should work now.

Related

Save Video SD Card

Hello How I can Save Video On external card I have android 4.4.2
File folder = new File(Environment.getExternalStorageDirectory() + "/Output");
if (!folder.exists()) folder.mkdir();
String out = Environment.getExternalStorageDirectory() + "/Output/" +
DateFormat.format("yyyy-MM-dd_kk-mm-ss", new Date().getTime()) +
".mp4";

Android: Creating folder does not work

I Want to take a photo and save it in the external storage, but the folder creation fails.
Permission is set.
File folder = new File(Environment.getExternalStorageDirectory() +
File.separator + "DCIM" + File.separator + "ScannerApp");
if(!folder.exists()){
folder.mkdirs();
}
Help please :)
use this code below to initialize folder value:
File folder = new File(Environment.getExternalStorageDirectory(), "DCIM" + File.separator + "ScannerApp");
or
File folder = new File(new File(Environment.getExternalStorageDirectory(), "DCIM"), "ScannerApp");
Replace this
File folder = new File(Environment.getExternalStorageDirectory() +
File.separator + "DCIM" + File.separator + "ScannerApp");
if(!folder.exists()){
folder.mkdirs();
}
with
File folder = new File(Environment.getExternalStorageDirectory().getPath() +
File.separator + "DCIM" + File.separator + "ScannerApp");
if(!folder.exists()){
folder.mkdirs();
}
The problem is that you are using Environment.getExternalStorageDirectory() which will not return the path. Use Environment.getExternalStorageDirectory().getPath() to get the path.
Hope this helps.

png transparent image file in android

I want to save a png image to SD card and it should be without background.
in the sd card I do not see any background but when attach it into telegram I see a white background. here is my code in android:
File filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath() + "/background_eraser/");
dir.mkdirs();
mImagename = "image" + cal.getTimeInMillis() + ".png";
file = new File(dir, mImagename);

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);
}
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.
replace this line
File file = new File (sdCard.getAbsolutePath() , File.separator + "/M3");
with this one.
File file = new File (sdCard.getAbsolutePath()+"/YourDirectoryName");

android programming question

dy_path = Environment.getExternalStorageDirectory() + "\5.jpg";
Instead of that i want how to give dynamic path automatically picture saved based on current time.
I am new to Android. Plz answer my question
You could concatenate the path with
DateFormat.getDateInstance().format(new Date());
That is, use something like
String time = DateFormat.getDateInstance().format(new Date());
dy_path = Environment.getExternalStorageDirectory() + "\\" + time + "\\5.jpg";
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/your folder name" + File.separator);
dir.mkdirs();
String pic = CommonMethod.getRandomString(30);
File file = new File(dir, String.valueOf(pic + ".jpg"));
picturePath = picturePath + String.valueOf(pic) + ".jpg";

Categories

Resources