我一直在开发一个关于HTC愿望的应用程序。它运行正常。单击按钮时的应用程序从我的Web服务器下载文件并将其保存到SD卡上。
我现在想在Samsung Galaxy Tab上运行此应用程序。但是,当我点击按钮时会崩溃。
我能看到的唯一原因是该标签不允许访问SD卡。它与HTC Deisre手机中的SD卡相同,不受写保护或其他任何东西?
任何人都知道什么是错的?
这是我用来下载文件并将其存储在卡上的代码......
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();
}
这在Desire上运作良好。
栈跟踪..
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)
答案 0 :(得分:4)
三星Galaxy Tab拥有大量内部闪存。因此Environment.getExternalStorageDirectory()
会返回/mnt/sdcard/
,但这实际上是内部存储空间。真正的外部存储位于/mnt/sdcard/external_sd/
。有关详细信息,请参阅this post。
答案 1 :(得分:2)
找到答案! 在Galaxy选项卡上,存储不仅是/ sdcard,而是/ sdcard / external_sd /
答案 2 :(得分:1)
我想这条线会造成麻烦:
new FileOutputStream(testDirectory+"/file.csv");
首先创建目录file
,但根据您的堆栈跟踪,您尝试访问SD卡根目录上的file.csv
。所以我想你不能简单地将file.csv附加到testDirectory
类型的File
变量。