[英]Image from camera not set to ImageView
Image taken from a camera is not set even though the Code I'm using is from the android developer website. 即使我使用的代码来自android开发者网站,也不会设置从相机拍摄的图像。 Please help.
请帮忙。 I don't get what I'm supposed to do.
我不知道我该怎么做。 Sometimes, the OnActivityResult method isn't called either.
有时,也不会调用OnActivityResult方法。 Here is the code I am using:
这是我正在使用的代码:
@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);
}
}
}
If you start an activity for result, the request code identifies the request. 如果为结果启动活动,则请求代码将标识请求。 So if you started camera activity with request code, REQUEST_TAKE_PHOTO then you must use the same request code in activity result ie
因此,如果您以请求代码REQUEST_TAKE_PHOTO开始摄像机活动,则必须在活动结果中使用相同的请求代码,即
onActivityResult 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);
}
}
UPDATE UPDATE
Since you want to decodeFile, your mCurrentPhotoPath
must be absolute file path and not uri.So remove "file:" from mCurrentPhotoPath
由于要解码文件,因此
mCurrentPhotoPath
必须是绝对文件路径,而不是uri。因此请从mCurrentPhotoPath
删除“ file:”
createImageFile 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;
}
Hope it helps you.. 希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.