【问题标题】:How to get images from gallery and display them to the screen in android sdk如何从图库中获取图像并在 android sdk 中将它们显示到屏幕上
【发布时间】:2010-08-20 17:18:06
【问题描述】:

我想知道如何从图库中获取预先保存的图像,然后将其显示到屏幕上。任何教程/有用的链接和信息将不胜感激。如果您有什么需要我解释的更多信息,请询问。

【问题讨论】:

    标签: android image sdk get


    【解决方案1】:
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, 1);
    

    此 Intent 用于从您的 SD 卡中选择图像并使用 onActivityResult() 获取图像并在 ImageView. 中显示图像

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case 1:
     {
      if (resultCode == RESULT_OK)
      {
        Uri photoUri = data.getData();
        if (photoUri != null)
        {
        try {
              String[] filePathColumn = {MediaStore.Images.Media.DATA};
              Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null); 
         cursor.moveToFirst();
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
     String filePath = cursor.getString(columnIndex);
     cursor.close();
     Bitmap bMap = BitmapFactory.decodeFile(filePath);
     image.setImageBitmap(bMap);
    
     }catch(Exception e)
      {}
      }
    }
    }
    }
    

    现在我们从图库中获取选择的图像,然后将图像设置为 ImageVIew。这里 image.setImageBitmap(bMap); 将图像设置为 ImageView。

    【讨论】:

    • 你应该经常检查 cursor.MoveToFirst 是否真的可以执行,否则如果没有媒体返回你会得到一个异常: if (cursor.moveToFirst()) {main code}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多