Java 在android中下载图像

Java 在android中下载图像,java,android,Java,Android,我已经做了一个应用程序,我需要从一个给定的链接下载一个图像。它在logcat中显示无错误和无错误消息。下面是我的代码 public Drawable getImage(String ImagePath,String fileName) { Log.v("download", "image downloading from net"); boolean bin = getBinaryWebFile(ImagePath,fileName); Drawable draw

我已经做了一个应用程序,我需要从一个给定的链接下载一个图像。它在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为null(注释为1)。我甚至在舱单上写了下面的任务

   <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"/>


在引用为注释1的draw中仍然为null

您是否在模拟器中运行应用程序?确保它具有正确的internet连接。有时模拟器可能会断开internet连接,尤其是当您在不重新启动模拟器的情况下更改网络时。

此代码适用于我:

private static long DownloadFromUrl(Context context, URL fileUrl,
        String fileName) throws Exception { // this is the downloader method

    try {

        URL myFileUrl = null;
        myFileUrl = fileUrl;

        // *** Internet connection
        HttpURLConnection conn = (HttpURLConnection) myFileUrl
                .openConnection();
        conn.setDoInput(true);
        conn.connect();
        // int length = conn.getContentLength();
        // int[] bitmapData =new int[length];
        // byte[] bitmapData2 =new byte[length];

        InputStream is = conn.getInputStream();

        // decodificar la imagen de internet en bmImg
        Bitmap bmImg = BitmapFactory.decodeStream(is);

        // ***Save in the the SD

        FileOutputStream out = new FileOutputStream(
                <path + filename>);

        byte[] buffer = new byte[1024];
        int len;
        while ((len = is.read(buffer)) > 0) {
            out.write(buffer, 0, len);
            tam += len;
        }
        is.close();

        // Save using the selected format and quality
        bmImg.compress(Bitmap.CompressFormat.PNG,
                <quality>, out);
        out.flush();
        out.close();

        // tam=bmImg.getByteCount();
        bmImg.recycle();

        return tam;

    } catch (IOException e) {
        // showErrorMessage(context, "Error downloading");
        e.printStackTrace();
        return 0;
    }

}
private static long DownloadFromUrl(上下文上下文、URL文件URL、,
字符串文件名)引发异常{//这是downloader方法
试一试{
URL myFileUrl=null;
myFileUrl=fileUrl;
//***互联网连接
HttpURLConnection conn=(HttpURLConnection)myFileUrl
.openConnection();
conn.setDoInput(真);
连接();
//int length=conn.getContentLength();
//int[]位图数据=新的int[length];
//byte[]bitmapData2=新字节[长度];
InputStream is=conn.getInputStream();
//在bmImg中对互联网图像进行解码
位图bmImg=BitmapFactory.decodeStream(is);
//***保存在SD中
FileOutputStream out=新的FileOutputStream(
);
字节[]缓冲区=新字节[1024];
内伦;
而((len=is.read(buffer))>0){
out.write(缓冲区,0,len);
tam+=len;
}
is.close();
//使用选定的格式和质量保存
bmImg.compress(Bitmap.CompressFormat.PNG,
,外出);
out.flush();
out.close();
//tam=bmImg.getByteCount();
bmImg.recycle();
返回tam;
}捕获(IOE异常){
//错误消息(上下文,“下载错误”);
e、 printStackTrace();
返回0;
}
}