无法在ImageView中显示图像

时间:2012-10-18 18:51:29

标签: android imageview gallery

我从图库中选择图像并在ImageView上显示图像。我能够成功地做到这一点。我的问题是,当我尝试设置从相机捕获的图像(.jpg图像)时,它们未在图像视图中显示,只是出现一个空白屏幕。甚至成功显示.jpg类型的图像也不是从相机捕获的。无法理解的原因,可能是由于尺寸较大。请帮忙 以下是我的代码:

public class ImagePick extends Activity {

private Bitmap bitmap;
private ImageView imv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_upload);
imv = (ImageView) findViewById(R.id.targetimage);

  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  intent.setType("image/*");
  startActivityForResult(intent, 0);

}


protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
     super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

     switch(requestCode) {
     case 0:
         if(resultCode == RESULT_OK){ 
             Uri selectedImage = imageReturnedIntent.getData();
             String[] filePathColumn = {MediaStore.Images.Media.DATA};

             Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
             cursor.moveToFirst();

             int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
             String filePath = cursor.getString(columnIndex); // file path of selected image
             cursor.close();
                   //  Convert file path into bitmap image using below line.
             Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);

                   // put  bitmapimage in your imageview
             imv.setImageBitmap(yourSelectedImage);
         }
     }
}
}

先谢谢

2 个答案:

答案 0 :(得分:0)

您需要使用FileInputStream和BufferedInputStream来读取图像,然后将图像转换为位图。

    private FileInputStream is = null;
    private BufferedInputStream bis = null;


is = new FileInputStream(new File(imagePath));
        bis = new BufferedInputStream(is);

还要检查link

由于

答案 1 :(得分:0)

而不是:

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex); // file path of selected image

尝试:

int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);

关闭光标 After BitmapDecoding。

请记住,应该在AsynckTasks中访问SD卡。并且您可以调整图像大小以适应屏幕,这样您就不会使用大量内存,请查看我的其他答案Bitmap Out Of Memory Issues。关于如何做到这一点。