0

我正在从图库中挑选图像并在 ImageView 上显示图像。我能够成功地做到这一点。我的问题是,当我尝试设置从相机(.jpg 图像)捕获的图像时,它们没有显示在 imageview 中,只是出现了一个空白屏幕。即使是 .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);
         }
     }
}
}

提前致谢

4

2 回答 2

0

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

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


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

还要检查链接

谢谢

于 2012-10-18T18:59:41.450 回答
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);

并在 BitmapDecoding之后关闭光标。

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

于 2012-10-18T19:11:56.420 回答