将图像从url转换为位图

时间:2012-12-20 09:59:51

标签: 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));
}

4 个答案:

答案 0 :(得分:3)

我正在使用以下代码,它在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;    
    }

答案 1 :(得分:0)

尝试这个

 private Bitmap getBitmap(String url) 
        {
            File f=fileCache.getFile(url);

            //from SD cache
            Bitmap b = decodeFile(f);
            if(b!=null)
                return b;

            //from web
            try {
                Bitmap bitmap=null;
                URL imageUrl = new URL(url);
                HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
                conn.setConnectTimeout(30000);
                conn.setReadTimeout(30000);
                conn.setInstanceFollowRedirects(true);
                InputStream is=conn.getInputStream();
                OutputStream os = new FileOutputStream(f);
                Utils.CopyStream(is, os);
                os.close();
                bitmap = decodeFile(f);
                return bitmap;
            } catch (Exception ex){
               ex.printStackTrace();
               return null;
            }
        }

 private Bitmap decodeFile(File f){
        try {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE=70;
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){
                if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }

            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }

答案 2 :(得分:0)

   URL url = new URL(src);
   HttpURLConnection connection;
   connection=(HttpURLConnection) url.openConnection();
   connection.setDoInput(true);
   connection.connect();
   InputStream input = connection.getInputStream();
   Bitmap myBitmap = BitmapFactory.decodeStream(input);

如需更多参考,请点击此处 http://androiddhina.blogspot.in/2015/01/get-bitmap-from-url-in-android.html

答案 3 :(得分:0)

尝试一下可能对您有用

  useEffect(() => {
      setList([...list, task]);
  }, [task]);