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();
}
}