Android: Creating folder does not work - android

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.

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

How to create Folder directory in Android API 30 or Android R version device

File file = new File(MATTApplication.getContext().getExternalFilesDir(
Environment.DIRECTORY_DOCUMENTS), "MATT_LOGS");//new File(Environment.getExternalStorageDirectory(), "MATT_LOGS");
if (!file.mkdirs()) {
//file.mkdir();
file.mkdirs();
}
filePath = file.getAbsolutePath() + File.separator + fileName;

Creating directory hierarchy in Android (for gallery)

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.

how to create application folder in android

I am creating application folder on first activity on onCreate() Method but folder is not creating.here is the code
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File fil = new File(this.getFilesDir().getPath()+File.separator+"MyContactsBackUp");
fil.mkdirs();
} else {
File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"MyContactsBackUp");
directory.mkdirs();
}
Try this code
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
File podcastFolder = new File(Environment.getExternalStorageDirectory() + File.separator
+ getString(R.string.app_name));
} else {
/* save the folder in internal memory of phone */
File podcastFolder = new File("/data/data/" + getPackageName()
+ File.separator + getString(R.string.app_name));
}
Try this.
File f = new File(android.os.Environment.getExternalStorageDirectory(),File.separator+"MyContactsBackUp/");
f.mkdirs();
or for internal memory of application instead of write hardcore sting use.
File f = new File(getCacheDir(),File.separator+"MyContactsBackUp/");
f.mkdirs();

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