从Url获​​取图像并保持其原始大小

时间:2015-06-16 08:21:52

标签: android

我使用此代码块从URL获取图片:

BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
//from web
try {
    Log.i(TAG,""+g.getPath());
    InputStream is = new URL(g.getPath()).openStream();
    BitmapFactory.decodeStream(is, null, bmOptions);
    is.close();
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = 2;
    o2.inMutable = true;
    is = new URL(g.getPath()).openStream();
    bitmap = BitmapFactory.decodeStream(is, null, o2);
    is.close();
} catch (Exception ex) {
    ex.printStackTrace();
}

下载成功,但图片尺寸不是原始尺寸。分辨率较低。如何从Url获​​取原始尺寸的图像?

4 个答案:

答案 0 :(得分:1)

删除此行

    o2.inSampleSize = 2;

您也可以使用                   bitmap = BitmapFactory.decodeStream(is,null,bmOptions);

无需创建新的BitmapOptions

答案 1 :(得分:0)

你的代码做了两次你应该做的一次。使用:

//from web
try {
    InputStream is = new URL(g.getPath()).openStream(); //open the stream
    bitmap = BitmapFactory.decodeStream(is, null); //decode it without options
    is.close();
} catch (Exception ex) {
    ex.printStackTrace();
}

请注意,根据图片,以全分辨率加载它可能会轻易破坏您的代码并抛出OutOfMemoryException。在这些情况下,你可以像你一样使用inSampleSize; inSampleSize = 2表示宽度和高度都是原始值的一半。

答案 2 :(得分:0)

来自网址获取 原始尺寸 图片的代码

初始化变量:

private ImageView mImageView;

绑定视图:

mImageView = (ImageView) findViewById(R.id.mImageView);

从网址获取图片的异步任务:

private class LoadImageInBackGround extends AsyncTask<String, Void, Drawable> {

        private String imageUrl , imageName;

        public LoadImageInBackGround(String url, String file_name) {
            this.imageUrl = url;
            this.imageName = file_name;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Drawable doInBackground(String... urls) {

            try {
                InputStream is = (InputStream) this.fetch(this.imageUrl);
                Drawable d = Drawable.createFromStream(is, this.imageName);
                return d;
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return null;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        private Object fetch(String address) throws MalformedURLException,IOException {
            URL url = new URL(address);
            Object content = url.getContent();
            return content;
        }

        @SuppressWarnings("deprecation")
        @Override
        protected void onPostExecute(Drawable result) {
            super.onPostExecute(result);
            mImageView.setBackgroundDrawable(result);
        }
    }

如何调用异步任务:

new LoadBackgroundMain("Image_Url","My_Image").execute();

您必须在清单文件中添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

完成

答案 3 :(得分:0)

我已经改变了你的代码

BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
//from web
try {
    Log.i(TAG,""+g.getPath());
    InputStream is = new URL(g.getPath()).openStream();
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = 0;
    bitmap = BitmapFactory.decodeStream(is, null, o2);
    is.close();
} catch (Exception ex) {
    ex.printStackTrace();
}