Android 将图像从url转换为位图

Android 将图像从url转换为位图,android,android-imageview,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)).exist

在我的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;    
    }
试试这个

 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;
    }
私有位图getBitmap(字符串url)
{
文件f=fileCache.getFile(url);
//从SD缓存
位图b=解码文件(f);
如果(b!=null)
返回b;
//从网络
试一试{
位图=空;
URL imageUrl=新URL(URL);
HttpURLConnection conn=(HttpURLConnection)imageUrl.openConnection();
连接设置连接超时(30000);
连接设置读取超时(30000);
conn.setInstanceFollowRedirects(真);
InputStream is=conn.getInputStream();
OutputStream os=新文件OutputStream(f);
Utils.CopyStream(is,os);
os.close();
位图=解码文件(f);
返回位图;
}捕获(例外情况除外){
例如printStackTrace();
返回null;
}
}
私有位图解码文件(文件f){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的FileInputStream(f),null,o);
//找到正确的刻度值。它应该是2的幂。
所需的最终int_尺寸=70;
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){

如果(width_tmp/2试试,这可能对你有用

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.image);

        String urlImg="https://appscp.dansyo.com/apps-upload/da8c2cdaceaf82e9f066264e38750c8e.jpeg";

            new LoadAsync().execute(urlImg);

    }

    public static Bitmap getBitmapFromURL(String src) {
        try {

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

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private class LoadAsync extends AsyncTask<String, Bitmap, Bitmap>{

        @Override
        protected Bitmap doInBackground(String... strings) {
            return getBitmapFromURL(strings[0]);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);

            imageView.setImageBitmap(bitmap);
        }
    }
}
public类MainActivity扩展了AppCompatActivity{
私人影像视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=findViewById(R.id.image);
字符串urlImg=”https://appscp.dansyo.com/apps-upload/da8c2cdaceaf82e9f066264e38750c8e.jpeg";
新建LoadAsync().execute(urlImg);
}
公共静态位图getBitmapFromURL(字符串src){
试一试{
URL=新URL(src);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream输入=连接。getInputStream();
返回BitmapFactory.decodeStream(输入);
}捕获(IOE异常){
e、 printStackTrace();
返回null;
}
}
私有类LoadAsync扩展了AsyncTask{
@凌驾
受保护位图doInBackground(字符串…字符串){
返回getBitmapFromURL(字符串[0]);
}
@凌驾
受保护的void onPostExecute(位图){
onPostExecute(位图);
设置图像位图(位图);
}
}
}

显示放置imageview的xml。