我做了一个应用程序,我需要从给定的链接下载图像。它在 logcat 中没有显示错误和没有错误消息。以下是我的代码
public Drawable getImage(String ImagePath,String fileName) {
Log.v("download", "image downloading from net");
boolean bin = getBinaryWebFile(ImagePath,fileName);
Drawable draw = Drawable.createFromPath(SavePath+fileName); //..........(1)
return draw;
}//getImage ends here*/
private boolean getBinaryWebFile(String inURL, String filename) {
File file = new File(SavePath,filename);
try {
URL url = new URL(inURL);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
con.setDoOutput(true);
con.connect();
InputStream is = con.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte[] data =new byte[1024];
int x = 0;
while((x=is.read(data,0,1024))>=0){
bout.write(data,0,x);
}
fos.flush();
bout.flush();
fos.close();
bout.close();
is.close();
return true;
} catch (MalformedURLException e) {
Log.d("PowerSaver","getBinaryWebFile->MalformedURLException: "+e.getMessage());
//e.printStackTrace();
} catch (IOException e) {
Log.d("PowerSaver","getBinaryWebFile->I/O exception: "+e.getMessage());
}
return false;
}//getbinarywebfile ends here
在调试一切都运行良好,最后我得到 draw as null (commented as 1) 。我什至在 manifest 中写了以下权限。
<uses-permission
android:name = "android.permission.INTERNET"/>
<uses-permission
android:name = "android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
android:name = "android.permission.ACCESS_WIFI_STATE" />
<uses-permission
android:name = "android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission
android:name = "android.permission.READ_PHONE_STATE"/>
仍然在 draw 中获得 null ,这被引用为评论 1。