ウェブサイトから画像を取得してSDカードに保存するコードの一部がありました。私がsdk1.5で開発したとき、次のコードは機能していました。ただし、android sdk 2.0 に変更した後は動作しませんでした。この行には問題があります。FileOutputStream fos = new 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());
}
}
エラーは 3 番目のキャッチで発生します。ただし、この行を移動すると
FileOutputStream fos = new FileOutputStream(filepath + "/" + this.filename);
2 回目の try/catch まで、2 回目の catch で発生します。これについて私を助けてもらえますか?