• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps unable to write file in device external memory

senar

Lurker
Apr 5, 2013
4
0
Hi,
I want to write an apk file in device external memory (Not in Ext SD card) by downloading url.using Environment.getExternalStorageDirectory() able to write in memory and the same working in some devices other than Samsung grand, Duos and samsung note 1.
Why its not working in those devices and how can i make my check generic for all devices.:thinking:


Senthil V
 
I agree with LogicWorX..Some code would be very handy. Here is another snippet that will help you out when guarding against the external storage not being available:

String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
} else {
// Something else is wrong. It may be one of many other states, but
// all we need to know is we can neither read nor write
}

Another helpful snippet:

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
// Make sure the Pictures directory exists.
path.mkdirs();

the Environment class has other directories available as well in the class as constants.
 
Upvote 0
Hi,
This is the code i'm using for stroring the file in memory;

if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
mExternalStorageAvailable = mExternalStorageWriteable = false;
}

if (mExternalStorageAvailable && mExternalStorageWriteable) {
String PATH = Environment.getExternalStorageDirectory()+ "/download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "update.apk");
if (outputFile.exists()) {
Log.d("11111", "delete");
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones