android programming question - android

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

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.

Give a specific predefined name when saving audio files in Android

How can I save a file to external storage with a predefined name for example like myfile_date.mp4?
For external storage we do something like this with basic intelligence..:
private String getFilename()
{
String timeStamp = new SimpleDateFormat("ddMMYYYY").format(new Date());
String MySound = "MySound";
String DefinedName = MySound+timeStamp;
filepath = Environment.getExternalStorageDirectory().getPath();
file = new File(filepath,AUDIO_RECORDER_FOLDER);
if(!file.exists()){
file.mkdirs();
}
return (file.getAbsolutePath() + "/" + DefinedName + file_exts[currentFormat]);
}

FileNotFoundException when I want to copy a file

I've wrote method below to copy my backup file to external storage
public Boolean Backup() {
try {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File sd = Environment.getExternalStoragePublicDirectory("");
File data = Environment.getDataDirectory();
String dbPath = "//data//" + "com.example.sqlitetest"
+ "//databases//" + "TestDB";
// Backup file name
Calendar calendar = Calendar.getInstance();
String backupName = calendar.get(Calendar.YEAR) + "-"
+ (calendar.get(Calendar.MONTH) + 1) + "-"
+ calendar.get(Calendar.DAY_OF_MONTH) + "-"
+ calendar.get(Calendar.HOUR_OF_DAY) + ":"
+ calendar.get(Calendar.MINUTE) + ":"
+ calendar.get(Calendar.SECOND);
String backupPath = "//BackupFiles";
File db = new File(data, dbPath);
File backup = new File(sd + backupPath, backupName);
if (!backup.exists())
backup.mkdirs();
FileChannel src = new FileInputStream(db).getChannel();
FileChannel dest = new FileOutputStream(backup).getChannel();
dest.transferFrom(src, 0, src.size());
src.close();
dest.close();
return true;
} else
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
I've added permissions to write and read external storage. The destination folder is created (sd + backupPath) but files no and throws FileNotFoundException! How can I solve this problem?
Thanks in advance
Try:
String dbPath = context.getApplicationInfo().dataDir + File.separator
+ "databases" + File.separator + "TestDB";
And instead of using //, try File.separator.
Your dbPath variable is already a full absolute path name, so can't be used in combination with a File object referring to your data directory, but only on its own. And you got that full path name by making poor assumptions anyway.
What you should do is obtain a File representing the data directory as you are, and use it in combination with a String containing just a filename (within that) and not an absolute directory path.
If you do decide you want directories under your data directory, you can do that, but will have to make sure they exist or create them.

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.

Categories

Resources