Mkdirs does not create folder in internal storage (Android) - android

I'm trying to create a folder in the internal storage of an app so I can download files to it. But it doesn't seem to work. I've been looking for a solution for hours and hours but I can't find one and it's starting to drive me crzay.
This the code to create the folder:
String intStorageDirectory = Environment.getDataDirectory().toString();
File folder = new File(intStorageDirectory, "testthreepdf");
folder.mkdirs();
This code always returns "DOES NOT EXIST":
File f = new File(Environment.getDataDirectory() + "/testthreepdf");
if(f.exists())
{
Log.i("testApp", f.toString() + "EXISTS");
}
else {
Log.i("testApp", f.toString() + " DOES NOT EXIST");
}
These are the android permissions:
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
Anybody know what's wrong with this code?

Environment.getDataDirectory() returns the top-level data folder shared by all apps. You can't create folders there. To create folders in your app's data folder, use getFilesDir() instead, like this:
String intStorageDirectory = getFilesDir().toString();
File folder = new File(intStorageDirectory, "testthreepdf");
folder.mkdirs();
You need a Context to call getFilesDir().
For more information about the difference between getDataDirectory() and getFilesDir(), see this StackOverflow question.

Another problem (though not specifically causing any bug) is that READ_INTERNAL_STORAGE and WRITE_INTERNAL_STORAGE are non-existent in Android. They have no effect and should be removed from your manifest.

Related

How to make a folder on external SDCARD (Which is inserted from out side the device)?

I need to create a folder on my external storage (inserted SD card). I tried in several ways, but it just create a folder on device storage. I have seen to create on some android app. Please help me by providing a solution. Thanks in advance.
File directory = new File(Environment.getExternalStorageDirectory() + "/boyan/");
if (!directory.exists()) {
directory.mkdirs();
}
Permissions are:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Do it like this
File file = new File(Environment.getExternalStorageDirectory(), "Folder_name");
if (!file.exists()) {
file.mkdirs();
}
I got the solution.
Here it is.
Environment.getExternalStorageDirectory()
Above code return "/storage/sdcard0", But if you want to make a folder on external SD Card you need "/storage/sdcard1". so try to do something like this
File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath().replace("0", "1") + "/boyan/");

how can i delete a folder on app deletion which was made by app outside the appfolder

I made an application from which I make a folder and save some files in it.
What I want is, when I delete the application the folder that I make outside from application should be deleted.
Add your manifest following permission
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
Then java code
File dir = getActivity().getFilesDir();
if (dir.isDirectory())
{
String[] subDir = dir.list();
for (int i = 0; i < subDir.length;i++)
{
new File(dir, subDir[i]).delete();
}
}
Follow this link to listen to when your application is being uninstalled, and in the callback, delete the folder using #sasikumar's code below. Hope this helps.

Can't Read Files in Sdcard in emulator

I have some problems in my App.
I just wanna read file in sdcard, so I put the file in /storage/sdcard/abcd (the folder I made by adb) in emulator.
File name is "1.14P", and I verify it is in that path. Its permission is 770.
But my problem is, I can't access sdcard by Application.
sampleFile = new File (Environment.getExternalStorageDirectory(), "abcd/1.14P");
sampleFile.exists() //return false
sampleFile2 = new File (Environment.getExternalStorageDirectory(), "tttt");
sampleFile2.mkdir() //return false
Sdcard is mounted well, I thought.
String state = Environment.getExternalStorageState(); //return "mounted"
I give the permission but I can't access to sdcard. Here is my manifest.xml. (part)
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hu.test004" >
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="ANDROID.PERMISSION.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
...(Activities)
I can't understand why it happens. Please give me some favor. Thanks.
SOLVED::I've got it!
The problem is, I declared permissions in capital letter, so App doesn't grant permissions.
I change
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="ANDROID.PERMISSION.READ_EXTERNAL_STORAGE"/>
to
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
and permission granted.
Thank you for your favor all.
I think the "," is part of the problem. The other is the "exists()" is for directories, not files.
See here:
new File(path) always actually creates a file on android?
contrary to the docs:
http://developer.android.com/reference/java/io/File.html
you may want to use isFile() instead.
Could you try adding "/" in front of your names??
e.g. "/abcd/1.14P"

Why can i not make a directory inside Environment.DIRECTORY_PICTURES?

This is my code
File selfieLocation = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),
"Daily Selfies");
boolean isDirectory = false;
if(!selfieLocation.isDirectory()) {
//Creates directory named by this file
selfieLocation.mkdir();
isDirectory = selfieLocation.isDirectory();
}
//array of strings
for(String selfiePath: selfieLocation.list()) {
selfies.add(selfiePath);
}
Basically what I am trying to do is create my own customizable directory inside of the standard directory in which to place pictures that are available to the user.
I looked at related threads and saw this one, Android: unable to create a directory in default pictures folder. However I made sure that I had a call to getExternal...., and not just have Environment.DIRECTORY_PICTURES as a parameter.
I also looked on here http://developer.android.com/guide/topics/data/data-storage.html#filesExternal and saw that I had the right method call/format to create a customizable folder in external memory. The docs example was
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
I stepped through my code and saw that the local variable isDirectory stayed at false even after the call to selfieLocation.mkdir(). Does anyone know why this directory cannot be created?
Try to create directory with File#mkdirs(), not File#mkdir(). The latter assumes that all parent directories are already in place, so it won't create directory if any of its parents don't exist.
Also, take a look at your permissions in AndroidManifest.xml. You need the following permissions in order to read/write the content on external storage:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
...
</manifest>
android.permission.READ_EXTERNAL_STORAGE isn't required for now, but it will be in the future releases of Android.

File.list and File.listFiles return NULL

in my app, the user can download different sound files. I use the downloadManager for this and everything is working fine here.
String url = localList.get(position).guid;
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(localList.get(position).title);
request.setTitle("Downloading Podcast");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.setNotificationVisibility(DownloadManager.Request.
VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir("/MyAudio/",
localList.get(position).title + ".mp3");
DownloadManager manager = (DownloadManager)
v.getContext().getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
The problem is, when i try to show all files in the directory with file.list or file.listfiles its returning NULL.
File f = new File("/MyAudio/");
File file[] = f.listFiles();
String filename[] = f.list();
I tried a lot of stuff, like changing the folder name and location, using request.setDestinationInExternalFileDir , writing and reading the folder with
File sdCard = Environment.getExternalStorageDirectory();
String folder = sdCard.getAbsolutePath() + "/MyAudio" ;
But nothing helped so far. I always get NULL returned by file.listFiles.
I use these permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
which should cover everything i need.
One last point: I write the file in an arrayadapter class and try to read the folder from my mainactivity, and i see the folder and files on my phone, so im sure they exist.
Thx in advance

Categories

Resources