Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是否创建专门用于检索图像的类?_Android_Android Intent_Android Activity - Fatal编程技术网

Android 是否创建专门用于检索图像的类?

Android 是否创建专门用于检索图像的类?,android,android-intent,android-activity,Android,Android Intent,Android Activity,我有一种从图库中检索图像的方法,对我来说效果非常好。我的问题是,我在三个不同的活动中使用它,我觉得我应该创建一个可以调用以获取图像的类 这是我的代码: private void getImage () { Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); // Start

我有一种从图库中检索图像的方法,对我来说效果非常好。我的问题是,我在三个不同的活动中使用它,我觉得我应该创建一个可以调用以获取图像的类

这是我的代码:

private void getImage () {
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // Start the Intent
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}

当我试图把它放在自己的类中时,我无法让它工作,即使在传递上下文时也是如此。看来我需要把它变成一项活动,如果是的话,我不认为把它分开有什么意义。我是否遗漏了一些可以让我将其放入自己的类并返回图像的内容


谢谢

您使用的是ActivityResult方法,它来自Activity。可能有一个类方法来执行ActivityResult中的代码。将类更改为包含两个单独的方法,因为如果单独的类包含onActivity,除非它是一个activity,否则它将无法工作

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

    mRLMenuParams.setVisibility(View.GONE);
    mRLImageParams.setVisibility(View.VISIBLE);

    bmSelectedImage = null;

    try {
        // When an Image is picked
        if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                && null != data) {
            // Get the Image from data

            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            // Get the cursor
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imgDecodableString = cursor.getString(columnIndex);
            cursor.close();
            ImageView imSelectedImage = (ImageView) findViewById(R.id.imageView_image);
            imSelectedImage.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
            Bitmap bmImage = Bitmap.createBitmap(BitmapFactory.decodeFile(imgDecodableString));
            bmSelectedImage = bmImage.copy(Bitmap.Config.ARGB_8888, true);
        } else {
            Toast.makeText(this, "You haven't picked Image", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
    }
}