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 = 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

在调试时一切都运行良好,最后我得到了绘制为空(注释为1)。我什至在 list 中编写了以下权限。

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

在绘图中仍然得到 null,这被引用为注释 1。

最佳答案

这段代码对我有用:

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;
    }

}

关于java - 在 Android 中下载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13001063/

相关文章:

android - 我应该如何在 Android 的 UI 中引用 "external storage"?

android - 支持多个 Android 屏幕 : Advantages vs. 具有所有密度的抽屉文件夹(图像)的缺点?

java - Camel 从 Tracer 到 BacklogTracer

java - 从 csv 文件行创建 Java 对象的开销是多少

java - 在 NetBeans 上使用 JDK 13 运行 Java 应用程序

Android:tabSelectedTextColor 不会更改 TabLayout 中选定选项卡的文本

java - 为什么 Selenium 使用我的默认选项卡组打开一个新的 Firefox 窗口?

java - Java中如何合并两个对象?

android - 从 Android web 查看历史记录中删除/删除特定的 url?

Android TalkBack 重复说 "Service [my app name]"