Downloaded Image Appears Very Blurry
I downloaded an image to use following this excellent article. My own image I'm using is a google map static image that is 300x400. I've fiddle with a few of the settings and have
Solution 1:
Is 300x400 not a good res for android?
it depends on original image orientation and a couple of other things like your layout and device display. Alternatively, you could try the following code to download an image, this works fine for me:
don't forget to add
<uses-permissionandroid:name="android.permission.INTERNET"></uses-permission>
to your Manifest
publicclassMainActivityextendsActivity {
private Button get;
private ImageView pic;
privatestaticfinalStringSRC="http://www.allindiaflorist.com/imgs/arrangemen4.jpg";
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
get = (Button)findViewById(R.id.button1);
pic = (ImageView)findViewById(R.id.imageView1);
get.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
pic.setImageBitmap(getBitmapFromURL(SRC));
}
});
}
publicstatic Bitmap getBitmapFromURL(String src) {
try {
URLurl=newURL(src);
HttpURLConnectionconnection= (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStreaminput= connection.getInputStream();
BitmapmyBitmap= BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("getBmpFromUrl error: ", e.getMessage().toString());
returnnull;
}
}
}
Solution 2:
I found the reason why they were low, the image loader class had a built in compression:
//decodes image and scales it to reduce memory consumptionprivate Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Optionso=newBitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(newFileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.finalint 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.Optionso2=newBitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(newFileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
returnnull;
}
So just remove this part so you are left with:
//decodes image and scales it to reduce memory consumptionprivateBitmapdecodeFile(File f){
try {
returnBitmapFactory.decodeStream(newFileInputStream(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
returnnull;
}
Crystal clear images.
Post a Comment for "Downloaded Image Appears Very Blurry"