I tested this code with Genymotion Marshmallow and with Nougat on my HTC 10, and it worked on both.
Now I tried Android 7.0 on Genymotion and it didn't create the directories.
Any idea why?
File file = new File(Environment
.getExternalStorageDirectory() + File.separator +
"SchoolAssist" + File.separator + lesson_name);
boolean isDir = file.exists();
if (!isDir)
isDir = file.mkdirs();
if (isDir) {
Intent notes = new Intent(getActivity(), NotesManager.class);
notes.putExtra("dir", file.getAbsolutePath());
startActivity(notes);
}
else
Toast.makeText(getContext(), "Error creating directory", Toast.LENGTH_SHORT).show();
Edit: My manifest contains these lines:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
In your code the Toast will be shown even when creating the directory worked. The Activity will only be started when the file already existed before calling your code.
Try this:
File file = new File(Environment
.getExternalStorageDirectory() + File.separator +
"SchoolAssist" + File.separator + lesson_name);
if(!file.exists()) {
if(file.mkdirs()) {
startNotesManager();
} else {
Toast.makeText(getContext(), "Error creating directory", Toast.LENGTH_SHORT).show();
}
} else {
startNotesManager();
}
And implement this helper method for starting the Activity:
private void startNotesManager() {
Intent notes = new Intent(getActivity(), NotesManager.class);
notes.putExtra("dir", file.getAbsolutePath());
startActivity(notes);
}
Related
This is my code
String DATA_PATH="/mnt/sdcard/";
String[] paths = new String[] { DATA_PATH, DATA_PATH + "tessdata/" };
for (String path : paths) {
File dir = new File(path);
if (!dir.exists()) {
if (!dir.mkdirs()) {
Log.v("", "ERROR: Creation of directory " + path + " on sdcard failed");
} else {
Log.v("", "Created directory " + path + " on sdcard");
}
}
}
I've tried using Environment.getExternalDirectory() but it still return false. The most confusing thing is it always said "ERROR: Creation of directory mounted on sdcard failed" on the logcat. How can the path changed into mounted? Can someone please give me a solution?
if the mobile is connected to the system then we are not able to create folders so remove it and run the application
Why are you using mkdirs in the first place ? You should just do:
File file = new File(Environment.getExternalStorageDirectory(), "tessdata");
if (!file.exists()) file.mkdir();
Also, make sure you have the WRITE_EXTERNAL_STORAGE permission in the manifest.
Hi i've been looking through various parts of my code to try and find out whats happening but can't seem to figure it out. The following code is supposed to be downloading two files one called "clientraw" and one called "clientrawextra" but for some reason when i look in the directory there are 2 versions of each file "clientraw...1..." "clientrawextra...1..."
Therefore it seems like it's downloading the files multiple times and i have no idea why??
Thanks in advance!
distance dis = new distance();
dis.findURL(value);
String url = dis.findURL(value);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "clientraw.txt");
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
/////////////////////////////////////////////////////
distance disextra = new distance();
disextra.findextra(value);
String urlextra = disextra.findextra(value);
DownloadManager.Request requestextra = new DownloadManager.Request(Uri.parse(urlextra));
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
requestextra.allowScanningByMediaScanner();
}
requestextra.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "clientrawextra.txt");
manager.enqueue(requestextra);
mDownload = new DownLoadComplte();
registerReceiver(mDownload, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
And the broadcast receiver...
private class DownLoadComplte extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(
DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
Intent myIntent = new Intent(splash.this, MainActivity.class);
myIntent.putExtra("key", value); //Optional parameters
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//unregisterReceiver(mDownload);
splash.this.startActivity(myIntent);
}
}
}
So if anyone has the same issue, apparently this is an ongoing and as of yet unresolved issue with the Download manager. I have used a little bit of a work around which you might want to use if your flow is similar to mine. Basically every time that the user opens the app two files are automatically downloaded to the SD card which overwrites the two files previously downloaded. So all i did was add a couple of extra functions to delete the duplicates...
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard.getAbsolutePath() +
"/Download/", client);
Log.d("file path", String.valueOf(file));
if(file.exists())
{
boolean flag = file.delete();
Log.d("file", "file deleted " + flag);
}
File sdCardextra = Environment.getExternalStorageDirectory();
File fileextra = new File(sdCardextra.getAbsolutePath() +
"/Download/", clientextra);
boolean exist = fileextra.exists();
Log.d("the file exists = ", String.valueOf(exist));
if(fileextra.exists())
{
boolean flag = fileextra.delete();
Log.d("file", "file deleted " + flag);
}
File sdCard2 = Environment.getExternalStorageDirectory();
File file2 = new File(sdCard2.getAbsolutePath() +
"/Download/", "clientraw-1.txt");
Log.d("file path", String.valueOf(file2));
if(file2.exists())
{
boolean flag = file2.delete();
Log.d("file", "file deleted " + flag);
}
File sdCardextra3 = Environment.getExternalStorageDirectory();
File fileextra3 = new File(sdCardextra3.getAbsolutePath() +
"/Download/", "clientrawextra-1.txt");
boolean exists = fileextra3.exists();
Log.d("the file exists = ", String.valueOf(exists));
if(fileextra3.exists())
{
boolean flag = fileextra3.delete();
Log.d("file", "file deleted " + flag);
}
I want to create a folder in SD card ,and i already add the permission
<user-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in manifest file.below is my code,but mkdirs return false! Can you help me!
File exportDir = new File(
Environment.getExternalStorageDirectory().toString(), "happydiarybackup");
if (!exportDir.exists()) {
boolean a = exportDir.mkdirs();
Log.d("mkdir ",exportDir.getAbsolutePath() + " make "+ a);
}
Try this. It might help you.
String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/happydiarybackup/";
try
{
File dir = new File(fullPath);
if (!dir.exists()) {
dir.mkdirs();
}
}
catch (Exception e) {
Log.e("App", "Exception" + e.getMessage());
}
1.Check your compileSdkVersion
2.Android: mkdirs()/mkdir() on external storage returns false.
Make sure your put the permission tag in.
i want to write to a file in the sdcard of my phone.i used the below code to do this.
private CSVWriter _writer;
private File _directory;
public String _fileTestResult;
private String PATH_FILE_EXPORT = "/applications/foru/unittestframework/";
public ExportData(){
_writer=null;
_directory = new File(Environment.getExternalStorageDirectory () +PATH_FILE_EXPORT);
if(!_directory.exists())
_directory.mkdirs();
}
public void exportResult(String testcaseNum,String testcase,String status){
try {
if(_directory.exists()){
//do something
}
but mkdirs() is not working.so i could not excecute following code in the if condition.please help me.
note:i have given the permission in manifest file.
EDIT:
i am using this file write option for storing the result of automation testing using robotium.i have created a normal project and tried to create directory in sdcard.but the same code when i am using in this testproject it is not working.why like that?dont unit testing framework support this?
have you add the correct permission in your manifest ?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Edit : ok, i just read your note for permission.
If it's help you this is my sdcard cache code :
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
String evtDir = "";
if(evt > 0){
evtDir = File.separator + evt;
}
cacheDir = new File(
android.os.Environment.getExternalStorageDirectory()
+ File.separator
+ "Android"
+ File.separator
+ "data"
+ File.separator
+ Application.getApplicationPackageName()
+ File.separator + "cache"
+ evtDir);
}else
cacheDir=context.getCacheDir();
if(!cacheDir.exists())
cacheDir.mkdirs();
}
Try below code
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()) {
imagefolder = new File(root,
mycontext.getString(R.string.app_name));
imagefolder.mkdirs();
}
} catch (Exception e) {
Log.e("DEBUG", "Could not write file " + e.getMessage());
}
Try with:
if(!_directory.exists())
_directory.mkdir();
Also check this - Creating a directory in /sdcard fails
I have this code
File dir = new File(Environment.getExternalStorageDirectory() + "/" + "new_dir");
if (dir.mkdir()) {
txtView.setText(dir + " Directory created");
} else {
txtView.setText(dir + " Directory is not created");
}
and i also added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
but it always goes in the else and the directory is never made :S :S
Try This code
String newFolder = "/myFolder2";
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File myNewFolder = new File(extStorageDirectory + newFolder);
(!myNewFolder.mkdir())
{
Log.e(TAG, "Create dir in sdcard failed");
return;
}
User permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />