我有两个图像按钮,一个用于相机,另一个用于图库。我想用相机拍摄照片或从图库中选择照片,并且该图像必须显示在图像视图中的另一个活动上。我的代码不起作用。
的manifest.xml
<manifest ...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera"></uses-feature>
</manifest>
第一项活动:
private static int REQUEST_CAMERA = 1;
private static int SELECT_FILE = 1;
String imagePath;
String filename;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton take_photo = (ImageButton) findViewById(R.id.cameraButton);
ImageButton get_photo = (ImageButton) findViewById(R.id.galleryButton);
take_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}
});
//to get the photo from gallery
get_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_FILE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
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]);
//file path of captured image
imagePath = cursor.getString(columnIndex);
//file path of captured image
File f = new File(imagePath);
filename = f.getName();
cursor.close();
Bitmap imageData = BitmapFactory.decodeFile(imagePath);
imageData = (Bitmap) data.getExtras().get("data");
Intent i = new Intent(this, ShowPhotoActivity.class);
i.putExtra("path", imagePath );
startActivity(i);
}
else if (requestCode == SELECT_FILE) {
//Bitmap photo = (Bitmap) data.getData().getPath();
Uri selectedImageUri = data.getData();
imagePath = selectedImageUri.getPath();
Bitmap imageData = BitmapFactory.decodeFile(imagePath);
imageData = (Bitmap) data.getExtras().get("data");
Intent intent = new Intent(this, ShowPhotoActivity.class);
intent.putExtra("name", imageData );
startActivity(intent);
} } }
}
SecondActivity:
ImageView showPhoto = (ImageView) findViewById(R.id.imageView);
image_path = getIntent().getStringExtra("path");
Bitmap bitmap = BitmapFactory.decodeFile(image_path);
showPhoto.setImageBitmap(bitmap);