有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Android Studio中选择图像后,java应用程序退出(未调用OnActivityResult)

所以我用这个打开我的图片,点击按钮选择

 //Open image chooser
                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, SELECT_PHOTO);

并用于ActivityResult

private final static int SELECT_PHOTO = 12345;

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("CALLED", "OnActivity Result");
        if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK) {
            // Let's read picked image data - its URI
            Uri pickedImage = data.getData();
            // Let's read picked image path using content resolver
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
            cursor.moveToFirst();
            String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
            Log.e("img", "It worked");

            // Do something with the bitmap


            // At the end remember to close the cursor or you will end with the RuntimeException!
            cursor.close();
        }
    }

在logcat中,OnactivityResult没有被调用,我不知道为什么。因此,当我点击按钮时,图像选择器弹出,我选择一个图像,然后它退出回到主屏幕。 我是否遗漏了什么,因为我遵循了其他人的代码,但我仍然得到同样的东西


共 (1) 个答案

  1. # 1 楼答案

    尝试一下:

       Intent intent = new Intent();
       intent.setType("image/*");
       intent.setAction(Intent.ACTION_GET_CONTENT);
      startActivityForResult(Intent.createChooser(photoPickerIntent,"Select:"), SELECT_PHOTO);