5

I have been developing an application on the HTC desire. It runs fine. The application when a button is clicked downloads a file from the my web server and saves it onto the SD card.

I now want to run this application on my Samsung Galaxy Tab. However, is crashes when i click on the button.

The only reason i can see is that the tab is not allowing access to the SD Card. It is the same SD Card i had in the HTC Deisre phone and it is not write protected or anything?

Anyone know what can be wrong?

This is the code i am using to download a file and store it on the card...

 class CreatefilesTask extends AsyncTask<Object, Integer, Boolean> {
    protected Boolean doInBackground(Object... arg0) {
        try{
            URL url  = new URL("http://213.143.36.32/file.csv");
            URLConnection conexion = url.openConnection();
            conexion.connect();
            int lenghtOfFile = conexion.getContentLength();
            InputStream is = url.openStream();
            File testDirectory = 
            new File(Environment.getExternalStorageDirectory()+"/File");
            if(!testDirectory.exists()){
                testDirectory.mkdir();
            }
            FileOutputStream fos = new FileOutputStream(testDirectory+"/file.csv");
            byte data[] = new byte[1024];
            int count = 0;
            long total = 0;
            int progress = 0;
            while ((count=is.read(data)) != -1){
                total += count;
                int progress_temp = (int)total*100/lenghtOfFile;
                if(progress_temp%10 == 0 && progress != progress_temp){
                    progress = progress_temp;
                }
                fos.write(data, 0, count);
            }
            is.close();
            fos.close();
        }
        catch(Exception e){
            e.printStackTrace();
        }

This works fine on the Desire.

StackTrace..

03-31 12:45:07.823: INFO/PowerManagerService(2494): Ulight 3->7|0
03-31 12:45:07.831: VERBOSE/WindowManager(2494): Delivering toWindow{48419208 com.android.qservices/com.android.qservices.AdminActivity paused=false}
03-31 12:45:07.898: VERBOSE/WindowManager(2494): Delivering toWindow{48419208 com.android.qservices/com.android.qservices.AdminActivity paused=false}
03-31 12:45:08.261: WARN/System.err(5314): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-31 12:45:08.261: WARN/System.err(5314):     at android.os.Handler.<init>(Handler.java:121)
03-31 12:45:08.265: WARN/System.err(5314):     at android.widget.Toast.<init>(Toast.java:77)
03-31 12:45:08.265: WARN/System.err(5314):     at android.widget.Toast.makeText(Toast.java:266)
03-31 12:45:08.269: WARN/System.err(5314):     at com.android.qservices.AdminActivity$CreatefilesTask.doInBackground(AdminActivity.java:117)
03-31 12:45:08.269: WARN/System.err(5314):     at com.android.qservices.AdminActivity$CreatefilesTask.doInBackground(AdminActivity.java:1)
03-31 12:45:08.269: WARN/System.err(5314):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-31 12:45:08.269: WARN/System.err(5314):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-31 12:45:08.273: WARN/System.err(5314):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-31 12:45:08.273: WARN/System.err(5314):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-31 12:45:08.273: WARN/System.err(5314):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-31 12:45:08.273: WARN/System.err(5314):     at java.lang.Thread.run(Thread.java:1096)
03-31 12:45:08.280: WARN/System.err(5314): java.io.FileNotFoundException: /mnt/sdcard/File/file.csv (No such file or directory)
03-31 12:45:08.284: WARN/System.err(5314):     at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
03-31 12:45:08.284: WARN/System.err(5314):     at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
03-31 12:45:08.288: WARN/System.err(5314):     at java.io.FileInputStream.<init>(FileInputStream.java:82)
03-31 12:45:08.288: WARN/System.err(5314):     at com.android.qservices.AdminActivity$CreatefilesTask.doInBackground(AdminActivity.java:148)
03-31 12:45:08.288: WARN/System.err(5314):     at com.android.qservices.AdminActivity$CreatefilesTask.doInBackground(AdminActivity.java:1)
03-31 12:45:08.288: WARN/System.err(5314):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-31 12:45:08.292: WARN/System.err(5314):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-31 12:45:08.292: WARN/System.err(5314):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-31 12:45:08.292: WARN/System.err(5314):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-31 12:45:08.296: WARN/System.err(5314):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-31 12:45:08.296: WARN/System.err(5314):     at java.lang.Thread.run(Thread.java:1096)
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Beginner
  • 28,539
  • 63
  • 155
  • 235
  • 1
    do you have the logcat output from the crash? – Will Tate Mar 31 '11 at 11:22
  • i get a warning message and a java io file not found exception – Beginner Mar 31 '11 at 11:29
  • 03-31 12:28:20.905: WARN/System.err(4700): java.io.FileNotFoundException: /mnt/sdcard/file.csv (No such file or directory) – Beginner Mar 31 '11 at 11:29
  • 03-31 12:28:20.909: WARN/System.err(4700): at java.io.FileOutputStream.(FileOutputStream.java:168) – Beginner Mar 31 '11 at 11:30
  • I dont actually know what all these means, im assuming it cant find the file which really should be there, also the file folder which should be created if it doesnt exist on the sd card is not being created – Beginner Mar 31 '11 at 11:31
  • please add the complete stack trace to the question and not at a comment! thanks – WarrenFaith Mar 31 '11 at 11:33
  • k done i have added it from the moment i click on the button to run the task – Beginner Mar 31 '11 at 11:49
  • @Beginner: Is SDCard remain accessible (writable actually) when you move it to Galaxy Tab? May be its getting mounted. – Mudassir Mar 31 '11 at 11:54
  • Whether i mount or unmount it, it does not work...I have also tried to coment all the code out and just make a directory on the card via the code and still not working – Beginner Mar 31 '11 at 11:59

3 Answers3

1

I guess that this line causes the trouble:

new FileOutputStream(testDirectory+"/file.csv");

You create a directory file first, but according to your stacktrace, you try to access file.csv on the root of the sdcard. So I guess that the you can't simply append a file.csv to your testDirectory variable of type File.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • If i put a breakpoint just before the application should make a directory on the sdcard called File...which it is not doing. This code is running fine on the HTC desire...would it function differently on the Galaxy in some way? – Beginner Mar 31 '11 at 11:39
1

Found the answer! On the Galaxy tab the storage is not only /sdcard it is /sdcard/external_sd/

Beginner
  • 28,539
  • 63
  • 155
  • 235
  • if you stumble on this, you should check your code if you used hardcoded paths. You should never do that! – WarrenFaith Mar 31 '11 at 12:14
  • I didnt use hardcode, i tried using Environment.getExternalStorageDirectory(), but for some reason that gives you /sdcard/, where in the galaxy it is sdcard/external_sd/ – Beginner Mar 31 '11 at 12:43
  • Maybe you should use `Environment.getExternalStorageDirectory().getPath()` this works for me on the galaxy tab... – WarrenFaith Mar 31 '11 at 12:54
  • Nope still doesnt work, i have to use...Environment.getExternalStorageDirectory()+"/external_sd/ – Beginner Mar 31 '11 at 14:08
1

Samsung Galaxy Tab has a good amount of internal flash memory. So Environment.getExternalStorageDirectory() returns /mnt/sdcard/ but this is actually the internal storage. The real external storage is in /mnt/sdcard/external_sd/. Please see this post for details.

Community
  • 1
  • 1
Mudassir
  • 13,031
  • 8
  • 59
  • 87