Am using sdcard for saving data ,my problem is that when app is running and some one delete this folder then the application fail. Is there anyway to notify the app , when this folder get deleted ?. Please help me
Try like this.
private static File logFile = null;
String SD_CARD_PATH = Environment.getExternalStorageDirectory().toString();
logFile = new File(SD_CARD_PATH + "/" + "folderName");
if (logFile.exists()){
//exist.so do your work
}
else{
//not exists
}
Related
Folder is creating Successfully but the location is something like storage/emualted/0
I'm using Enviroment.getExternalStorageDirectory methods
I want to create publicily available folder which i could save my app's data what should i do?
use Context#getFilesDir() orContext#getCacheDir()
it was restricted from google. below android 7 only u can create a folder inside internal folder. after that u only can create folder inside downloads,music and alarm
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(folder, "example.txt");
if (!file .exists()){
file .mkdirs();
}else{
}
Finally it worked in Android 10
//by Using This Method
private void createFolder(String name) {
File mydir = getApplicationContext().getExternalFilesDir(name); //Creating an internal directory;
if (!mydir.exists()) {
mydir.mkdirs();
Toast.makeText(this, "Created" + " -> " + mydir.getAbsolutePath(), Toast.LENGTH_SHORT).show();
} else {
}
I have to export files from my application and looking for a solution, where I can save files, to give the user the possibility to open them.
I tried already getFilesDir().getPath() which worked well, until I realized that the folder can't open from a real device (/data/user/0/com.myapplication.example/files) since the /data path is just a storage area for the application.
What are the alternatives?
You should have a look here https://developer.android.com/training/data-storage
I'm not sure what file type you are trying to store however what you have tried stores the file withing the applications directory and not the devices. To combat this I would look under either Media or Documents and other files again in the above link. I would be able to be of further assistance if I knew what file type you are trying to store. Hope this helps you in some way.
This is a function to store a float array to the phone external storage. Pass the file name.extension in the String name. You could modify it to export your file.
public static void save(float[] input_array, String name)
{
final String TAG2 = "->save()";
String string_array = Arrays.toString(input_array);
String fullName = Environment.getExternalStorageDirectory().getPath() + "/SercanFolder/" + name;
String path = Environment.getExternalStorageDirectory().getPath() + "/SercanFolder";
File folder = new File(path);
if(!folder.exists())
{
folder.mkdirs();
}
BufferedWriter buf;
try
{
buf = new BufferedWriter(new FileWriter(fullName));
buf.write(string_array,0,string_array.length());
buf.close();
Log.d(TAG+TAG2, "array saved as document. ");
}
catch (IOException e)
{
Log.e(TAG+TAG2, "problems while saving the file. ");
}
}
The suggestion with getExternalStorage().getPath() (Thanks to blackapps) helped me to save the pdf in a folder, which can be opened in the file manager.
Thanks for others help , i can create folder succeed now.
But i get a strange issue if android emulator doesn't has sdcard, i can't create the folder.
I can't figure it out..
Here is my code:
try {
if (Environment.getExternalStorageState()
.equals(Environment.MEDIA_MOUNTED)) {
System.out.println("can be read and write");
File sdFile = android.os.Environment.getExternalStorageDirectory();
//String path = sdFile.getPath() + File.separator + "DestPdf";
String path = sdFile.getPath() + "/DestPdf";
File dirFile = new File(path);
if (!dirFile.exists()) {// if folder doesn't exist
System.out.println("create file");
dirFile.mkdirs();// create file
System.out.println(dirFile.toString());
}
}
} catch (Exception ex) {
ex.toString();
}
If my android emulator has sdcard root, i can create the folder DestPdf:
If there is no sdcard , i print the root is /storage/emulated/0/DestPdf, i create the folder failed, the folder emulated has nothing...
Any help would be appreciated . Thanks in advance.
you can use your app Internal Package folder for saving data,
context.getFilesDir().getPath()
context is here Activity instance.Ok?
Do with it..
I've installed "android file transfer" on mac. My app creates a folder.I can access it using standard file manager in my android device. It shows many folders, including my folder.
But the problem is that "android file transfer" shows me my folder as a file.
Unplugging the device or re-launch of "android file transfer" didn't help.
this is how i create my file:
File folder = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() +File.separator + "myFolder/");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
and I read that I needed this :
MediaScannerConnection.scanFile(
getApplicationContext(),
new String[]{folder.getAbsolutePath()},
null,
new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
Log.v("tretretre",
"file " + path + " was scanned seccessfully: " + uri);
}
});
Can someone help me?
Late response, but I hope it can help someone.
I had same problem today. Seems that Android File Transfer app is handling last level folder's created by our apps as a file.
The only solution that has worked for me is to add a new last level folder inside the original one.
Instead of:
File folder = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() +File.separator + "myFolder/");
Do:
File folder = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() +File.separator + "myFolder/" + "myDummyFolder");
It could also be a hidden folder/file, like .myDummyFolder to avoid display it in file manager apps.
I m stuck with an issue that I want to copy/move a video from one location to another inside sdcard in android.
does anybody know how to do that.?
private String CopyFile(String srcPath,String destPath)
{
String videoFileName = VIDEO_FILE_PREFIX + mCurrentQuestion.getQuestionId() + VIDEO_FILE_SUFFIX ;
File source= new File(srcPath);
File destination= new File(destPath+"/"+videoFileName);
source.renameTo(destination);
return destPath+"/"+videoFileName;
}