android - 将图像从 url 转换为位图

标签 android android-imageview

在我的 android 应用程序中,当我尝试转换图像 url 位图时,图像的大小越来越小。

InputStream imageS = new URL(url).openConnection().getInputStream();
FileOutputStream fos = new FileOutputStream(fileName);
Bitmap.createBitmap(BitmapFactory.decodeStream(imageS));

if ((new File(extra)).exists()) {
   splashImage.setImageURI(Uri.parse(extra));
}

最佳答案

我正在使用以下代码,它出现在 URL 中。可能对您有用。

private Bitmap DownloadImage(String URL)
    {        
//      System.out.println("image inside="+URL);
        Bitmap bitmap = null;
        InputStream in = null;        
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
//        System.out.println("image last");
        return bitmap;                
    }
    private InputStream OpenHttpConnection(String urlString)
            throws IOException
            {
                InputStream in = null;
                int response = -1;

                URL url = new URL(urlString);
                URLConnection conn = url.openConnection();

                if (!(conn instanceof HttpURLConnection))                    
                    throw new IOException("Not an HTTP connection");

                try{
                    HttpURLConnection httpConn = (HttpURLConnection) conn;
                    httpConn.setAllowUserInteraction(false);
                    httpConn.setInstanceFollowRedirects(true);
                    httpConn.setRequestMethod("GET");
                    httpConn.connect();

                    response = httpConn.getResponseCode();                
                    if (response == HttpURLConnection.HTTP_OK) 
                    {
                        in = httpConn.getInputStream();                                
                    }                    
                }
                catch (Exception ex)
                {
                    throw new IOException("Error connecting");            
                }
                return in;    
    }

关于android - 将图像从 url 转换为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13969526/

相关文章:

java - 如何使用 fragment 添加时间选择器?

java - android - 什么是消息队列原生轮询在 android 中一次?

Android 谷歌地图显示为图片

android - 如何在 android 中使用 Piccaso 图像加载库平滑图像加载?

android - ImageView 不显示某些图像

android - Gradle 构建时间太慢

android - 是否有任何代码可以在不裁剪和缩放android的情况下设置墙纸?

android - 将数据从 SQLite 数据库加载到 ListView

java - Android+Java 从服务器上的 URL 获取图像并作为字符串传递给客户端 : Base64 decode and encode not working

android - 如何在 Android 中清除 ImageView?