karthik karthik - 5 months ago 43
Android Question

bitmap set to imageview is not working android

I am trying to add image to imageview of the listview based on Event type selected by the user,but The image is not reflecting in the imageview . Logcat doesn't shows any error
Pls help me to solve this problem

alertBuilder.setPositiveButton("ok",new DialogInterface.OnClickListener() {
@SuppressWarnings("null")
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if (spnView.getSelectedItem().toString().equals("User defined")) {
showDialog(USER_DEFINED_EVENT_NAME);
}
else{
ContentValues values=new ContentValues();
values.put(BirthdayProvider.EVENT_TYPE, spnView.getSelectedItem().toString());
int count=getContentResolver().update(BirthdayProvider.CONTENT_URI, values, BirthdayProvider.NUMBER+"='"+SearchListActivity.longClickValue+"'", null);
if (count==1) {
String iconType = null;
if (spnView.getSelectedItem().toString().equals("Birthday")) {
iconType="birthday_icon.png";
}
else if (spnView.getSelectedItem().toString().equals("Anniversary")){
iconType="rings_icon.png";
}
InputStream is = null;
try {
is = getResources().getAssets().open(iconType);
} catch (IOException e) {
e.printStackTrace();
}
Log.v("is......", is.toString());
Bitmap bit =BitmapFactory.decodeStream(is);
Log.v("bit......", bit.toString());
imgView.setImageBitmap(bit);
imgView.setScaleType(ScaleType.FIT_XY);
//finish();
Toast.makeText(getBaseContext(),"Updated Successfully",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getBaseContext(),"Updation Failed",Toast.LENGTH_SHORT).show();
}
}
}
});
alertDialog=alertBuilder.create();
alertDialog.show();
return alertDialog;
}


Logcat log

is...... android.content.res.AssetManager$AssetInputStream@42b32d18
bit..... android.graphics.Bitmap@42b35440

Answer

Use this to set image to imageview in your activity onResume() :-

profileImage = (ImageView) findViewById(R.id.profileImage);
new DownloadImageTask(profileImage).execute("");

Asynctask :-

class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImageTask(ImageView bmImage) {
 this.bmImage = bmImage;
}

protected Bitmap doInBackground(String... urls) {
 String urldisplay = urls[0];
 Bitmap mIcon11 = null;
try {
 InputStream in = new java.net.URL(urldisplay).openStream();
 mIcon11 = BitmapFactory.decodeStream(in);

float h = mIcon11.getHeight();
float w = mIcon11.getWidth();

float ratio = w / h ;

System.out.println("height is r :- "+h);
System.out.println("width is r :- "+w);
System.out.println("ratio is w/h:- "+ratio);

} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}

return mIcon11;
}

protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
//bmImage.setImageBitmap(Bitmap.createScaledBitmap(result, 250, 250, false));
//bmImage.setImageBitmap(getResizedBitmap(result, 250, 250));
}

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);


// RECREATE THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
}