我有一部分代码,这些代码应该从网站获取一个图像并将其存储到sdcard中。当我在sdk1.5上开发时,下面的代码正在使用find。但是,在我将其更改为AndroidSDK2.0之后,它现在就无法工作了。这一行出了问题: FileOutputStream fos =新FileOutputStream(filepath + "/“+ this.filename);
下面是我的代码:
void downloadFile(String fileUrl) {
URL myFileUrl = null;
try {
myFileUrl = new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String filepath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
FileOutputStream fos = new FileOutputStream(filepath + "/"
+ this.filename);
bmImg.compress(CompressFormat.JPEG, 75, fos);
fos.flush();
fos.close();
Context context = this.getBaseContext();
new MediaScannerNotifier2(context, filepath + "/" + this.filename,
"image/jpeg");
// displaying download completion message
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Wallpaper Downloaded").setCancelable(false)
.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
btn_setwall.setEnabled(true);
btn_download.setEnabled(false);
}
});
AlertDialog alert = builder.create();
alert.show();
} catch (Exception e) {
Log.e("MyLog", e.toString());
}
}错误发生在第三次捕获。但是,当我移动这条线时
FileOutputStream fos =新FileOutputStream(filepath + "/“+ this.filename);
对于第二次尝试/捕捉,那么它将发生在第二次捕获。能帮我一下吗?
发布于 2010-10-07 18:11:12
也许试着摆脱.getAbsolutePath()
这对我来说是适用于2.2的:
FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + fileName);https://stackoverflow.com/questions/2719661
复制相似问题