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();
Related
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;
my app save files in folder Documents. But I can't find this files in my telephone. What the problem?
File file = new File(getFilesDir() + DIR_NAME + fileName + ".txt");
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
}
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos, "UTF-8");
osw.write(editText.getText().toString());
File cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "folder-name"+"file-name");
//for make the directory if not exist
if (!cacheDir.exists())
//check file and other code...
android.os.Environment.getExternalStorageDirectory()
is used to check files in phone internal memory.
I am have downloaded some images into my sd card and i would like to NOT display them in my gallery. Is there a way to remove them? I have read about .nomedia file but how to do I create them?
is it by doing this?
File storagePath = new File(Environment.getExternalStorageDirectory(),folderName+"/Covers/");
if (!storagePath.exists())
{
File file = new File(storagePath, ".nomedia");
if (!file.exists()) {
try {
file.createNewFile();
Log.d("created","successful");
}
catch(IOException e) {
}
}
}
Here's a snippet that will do what you want:
File storagePath = new File(Environment.getExternalStorageDirectory(),
folderName + "/Covers");
storagePath.mkdirs();
File filename;
filename = new File(storagePath + "/" + ".nomedia");
if (!filename.exists()) {
filename.createNewFile();
}
Note: It might throw an IOException if it cannot create the file, you should catch this.
It will take likely a reboot for the mediascanner to rescan and determine it should not index that location.
I used a tutorial to download a zip into a subdirectory of my application's internal storage. I wrote the zip to /data/data/my.package.name/files/mySubDirectory/the.zip.
But, when I check to see whether the zip exists, it doesn't:
String fileDirectory = this.getFilesDir().getAbsolutePath() + "/mySubDirectory/the.zip";
File file = new File(fileDirectory);
if(file.exists()) {
Log.e(this.class.getName(), "file exists");
} else {
Log.e(this.class.getName(), "file doesn't exist");
}
I verified that fileDirectory is the same path as the File outFile for the FileOutputStream.
What could be the problem?
Try getting your file path as below :
String fileDirectory=Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "data" + File.separator + "data" + File.separator+ getActivity().getPackageName()+ File.separator +"mySubDirectory"+File.separator+"the.zip";
Using this SO question, I created a subdirectory using this example:
File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
File fileWithinMyDir = new File(mydir, "myfile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file
The problem is that I didn't expect the subdirectory to be prepended with "app_", so I was looking for the zip in the wrong place.
Try using getFilesDir() + "/" subdirectory + "/" "the.zip"
Without the getabsolutepath().
That is what I used could be the issue.
OK maybe you problem is with permissions do you see the file in the DDMS under data/data/package/files ? Check the permissions for the files
Here is my code
String path = getFilesDir() + "/"
+ subDirName + "/";
File file = new File(path);
file.mkdirs();
setReadable(file);
I use the following to make the file readable
#TargetApi(9)
private void setReadable(File file) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
try {
Runtime.getRuntime().exec(
"chmod 777 " + file.getCanonicalPath());
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
file.setReadable(true, false);
}
}
}
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");