位图设置为imageview不能正常工作

时间:2014-02-03 14:53:05

标签: android

我正在尝试根据用户选择的事件类型将图像添加到列表视图的图像视图,但图像未反映在图像视图中。 Logcat不会显示任何错误 请帮我解决这个问题

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日志

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

1 个答案:

答案 0 :(得分:1)

使用此功能在您的活动onResume()中将图像设置为imageview: -

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;
}
}