我需要创建一个不扩展Activity的辅助类。此代码用于根据RESULT从相机和图库加载。
我不了解如何使用上下文从其他类调用该类(因为它不扩展活动) 请你帮帮我吧?
下面是我需要提取的代码,并使用上下文在另一个类中调用它。
enter code here
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.default_image);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK && null != data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
Log.i(TAG, "destination= " + destination);
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
img.setImageBitmap(thumbnail);
}
我创建了一个名为ActivityResultHelper的新类,但遇到了findViewById的问题 活动结果助手
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
*
*/
public class ActivityResultHelper{
ImageView img;
int RESULT_LOAD_IMAGE = 1;
int REQUEST_CAMERA=2;
int RESULT_OK=3;
Context context;
public ActivityResultHelper(Context _context){
this.context = _context;
}
//@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
String TAG = ImageUtil.class.getSimpleName();
//super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
// ImageView imageView = (ImageView)context.findViewById(R.id.default_image);
// imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK && null != data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
Log.i(TAG, "destination= " + destination);
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
img.setImageBitmap(thumbnail);
}
}
}