即使我使用的代码来自android开发者网站,也没有设置从相机拍摄的图像。请帮忙。我不明白我应该做的事情。有时,也不会调用OnActivityResult方法。这是我正在使用的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
mImageView = (ImageView) findViewById(R.id.imageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispatchTakePictureIntent();
}
});
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
System.out.println("Created: " + mCurrentPhotoPath);
galleryAddPic();
return image;
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
private void setPic() {
// Get the dimensions of the View
// int targetW = mImageView.getWidth();
// int targetH = mImageView.getHeight();
//
// // Get the dimensions of the bitmap
// BitmapFactory.Options bmOptions = new BitmapFactory.Options();
// bmOptions.inJustDecodeBounds = true;
// BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
// int photoW = bmOptions.outWidth;
// int photoH = bmOptions.outHeight;
//
// // Determine how much to scale down the image
// int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
//
// // Decode the image file into a Bitmap sized to fill the View
// bmOptions.inJustDecodeBounds = false;
// bmOptions.inSampleSize = scaleFactor;
// bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
mImageView.setImageBitmap(bitmap);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d("", "onActivityResult: "+mCurrentPhotoPath);
if (requestCode == REQUEST_IMAGE_CAPTURE) {
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
mImageView.setImageBitmap(bitmap);
}
}
}
答案 0 :(得分:0)
如果您为结果启动活动,请求代码会标识请求。因此,如果您使用请求代码REQUEST_TAKE_PHOTO启动了摄像机活动,那么您必须在活动结果中使用相同的请求代码,即
<强> onActivityResult 强>
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d("", "onActivityResult: "+mCurrentPhotoPath);
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
mImageView.setImageBitmap(bitmap);
}
}
<强>更新强>
由于你想要decodeFile,你的mCurrentPhotoPath
必须是绝对文件路径而不是uri。所以删除&#34;文件:&#34;来自mCurrentPhotoPath
<强> createImageFile 强>
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
System.out.println("Created: " + mCurrentPhotoPath);
galleryAddPic();
return image;
}
希望它可以帮助你..