2

I've developed an application in which user can download .mp3 files from server. And pre-defined a path to mnt/sdcard/foldername for saving such files. I had run my program in HTC, LG, Samsung works perfect but when I running a same program at samsung galaxy s2 getting an issue that can't able to write(store) in mnt/sdcard/foldername and tried

 Environment.getExternalStorageDirectory().getAbsolutePath()

but its shows downloaded file names in given path and zero bytes for each files properties. Any idea to solve this issue?

arshad kr
  • 357
  • 3
  • 16

3 Answers3

1

The SG2 does usually not have a sd-card and uses the internal flash memory as "external" storage. I have solved this issue with this code:

    private File initCacheDir() {
        String sdState = android.os.Environment.getExternalStorageState();
            File imageCacheDir;
            if (sdState.equals(android.os.Environment.MEDIA_MOUNTED)) {
                File sdDir = android.os.Environment.getExternalStorageDirectory();      
                imageCacheDir = new File(sdDir, "Android/data/" + App.PACKAGE_NAME + "/files/imageCache");
            }
            else
                imageCacheDir = context.getCacheDir();

            if(!imageCacheDir.exists())
                 imageCacheDir.mkdirs();        
            return imageCacheDir;
}

Note that this code give you the location of the cache directory, which is usually located in the Android/data folder on the sd-card.

You'll find more details how to solve this issue with SG2 here: How could i get the correct external storage on Samsung and all other devices?

Community
  • 1
  • 1
bjorncs
  • 1,250
  • 11
  • 20
  • yup! thnx for your reply but I meant permanent storage in external card if I use /data path, it can't store more number of files or large spaced files.. also it seems like temp storage. thank you! – arshad kr Aug 24 '12 at 13:29
1

try this

 if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"yourfile");
    else
        cacheDir=context.getCacheDir();
    if(!cacheDir.exists())
        cacheDir.mkdirs();
Athul Harikumar
  • 2,501
  • 18
  • 16
  • Thank you! still gotta issue. My problem is not to make directory,have to download to sdcard. I tried with the same code to download an image, its work. If I go for mp3, its lacks. This problem occurs only with samsung galaxy s2. As @bjorncs said/ it uses flash memory where am struggling with it. – arshad kr Aug 24 '12 at 13:36
1

I finally found the code

public void download(String urlToDownload){

URLConnection urlConnection = null;

try{

    URL url = new URL(urlToDownload);

    //Opening connection of currrent url

    urlConnection = url.openConnection();
    urlConnection.connect();

    //int lenghtOfFile = urlConnection.getContentLength();


String PATH = Environment.getExternalStorageDirectory() + "/1/";

File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "file.mp3");
FileOutputStream fos = new FileOutputStream(outputFile);

InputStream is = url.openStream();


byte[] buffer = new byte[1024];

int len1 = 0;

while ((len1 = is.read(buffer)) != -1) {
    fos.write(buffer, 0, len1);
}

fos.close();
is.close();

System.out.println("downloaded"+urlToDownload);

}catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();

}

}

Source: link

Community
  • 1
  • 1
arshad kr
  • 357
  • 3
  • 16