从图库Android Studio获取图像

时间:2018-07-25 17:40:34

标签: android android-studio android-gallery

我正在尝试在Android Studio中创建一个应用程序,允许用户从其图库中选择图片。到目前为止,我的代码是:

df <- data.frame(food = c("Coffee","Coffee","Turkey", NA), 
             utensil = c(NA, "Cup", "Fork", "Fork"), 
             name = c("Steve", NA, NA, "Mike"))
df <- df %>%
  group_by(food) %>%
  arrange(utensil) %>%
  fill(utensil) %>%
  group_by(utensil) %>%
  arrange(food) %>%
  fill(food) %>%
  drop_na()

这不起作用。当我运行该应用程序并单击该按钮以选择图像时,它将导致该应用程序崩溃。没有错误信息。我的代码有问题吗?

Logcat

public void openGallery ()
{
    Intent intent = new Intent();
    // Show only images, no videos or anything else
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    // Always show the chooser (if there are multiple options available)
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        Uri uri = data.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            // Log.d(TAG, String.valueOf(bitmap));

            ImageView imageView = (ImageView) findViewById(R.id.chosenImage);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

错误是说它找不到方法openGallery(View ),而您里面只有openGallery(),其中没有View参数。由于您是在XML中引用该方法,因此应将该方法签名更改为openGallery(View v)