Reputation: 137
Right now I am trying to make an application that basically creates 100 files on SD card using a for loop.
I have looked into a lot of related questions and posts and I can't happen to solve it myself. I am using Android Studio's built-in emulator as my testing device since I prefer not to run it on my actual phone...
Anyway I created a button which if you click, it produces 100 files on external storage (well that's my intention though -_-).
So far I have this,
for (int i=1; i <= 100; i++) {
sdPath = Environment.getExternalStorageDirectory() + "/Android/";
File file = new Files(sdPath + "hacked" + i);
try{
FileOutputStream fos = new FileOutputStream(file);
fos.close();
}catch (Exception e){Log.i("Failed to save", e.getMessage());
}
This is the main implementation of file creation so far. I have edited my Manifest to include permission to write external storage but every time when I run the application I get /sotrage/0B01-3415/Android/hacked 1-100: open failed: EACCESS (Permission denied)
message.
Does anyone happen to know what is causing the trouble???
Also what I don't understand is, when we use Environment.getExternalStorageDirectory
, what does it really mean? In my actual phone, directories are like /sdcard/Android/data
.... like this form.
Upvotes: 0
Views: 1126
Reputation: 1604
Try with following code:
String path = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
for (int i=1; i <= 100; i++) {
File file = new File(path, "/Android/" + "hacked" + System.currentTimeMillis() + "");
try {
if (!file.exists()) {
file.createNewFile();
}
} catch (Exception e) {
String errorMessage = "Your device doesn't support this action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
Declare following permissions in Manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Upvotes: 0
Reputation: 3398
This is your need
public void createFiles(View view)
{
String path = new String(Environment.getExternalStorageDirectory().getAbsolutePath() + "/AndroidSample/");
boolean res;
for(int i = 1; i <= 100; i++) {
path += String.valueOf(i);
File file = new File(path);
if (!file.exists()) {
res = file.mkdirs();
} else {
res = false;
}
if(res == true)
Log.d("File Created", String.valueOf(i));
}
}
Add These Two Permissions on Manifest xml file android.permission.WRITE_EXTERNAL_STORAGE and android.permission.READ_EXTERNAL_STORAGE
To add permission in manifest refer this
Remember file names are going like 1, 12, 123,....
Upvotes: 1
Reputation: 2191
Have you declared permission at right place in manifest file as
<manifest>
<application>
...
</application>
<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"/>
</manifest>
Hope it helps you.
Upvotes: 0