android : 从 Camera 捕获图像它得到一个 null
android : capturing image from Camera it get a null
我在 onActivityResult() 中收到 "photoFile" is null
单击捕获按钮时,我调用了一个方法 dispatchTakePictureIntent()
但是当我 return 返回时它得到空值。
当图像分辨率/尺寸非常大(如 4012*4012 等)时会出现此问题,特别是在 Android 6.0 版 +
上会出现此问题
这是我的代码
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(
android.provider.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 = Util.createImageFile();
mCurrentPhotoPath = photoFile.getAbsolutePath();
System.out.println( "photoFile:" + photoFile.getAbsolutePath());
} catch (IOException ex) {
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
System.out.println("@@@@ photoFile: " + photoFile.getAbsolutePath());
/*takePictureIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT,1024);
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
mCurrentPhotoPath);*/
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_CANCELED) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
//Bundle extras = data.getExtras();
// Log.i(TAG, "extras: " + extras.get("data"));
Bitmap mphoto = (Bitmap) data.getExtras().get("data");
System.out.println( "***photoFile: " + data.getExtras().get("data"));
} catch (Exception e) {
// Log.i(TAG, "Exception:" + e.getMessage());
}
ShowPopupDialog(mCurrentPhotoPath);
}
}
}
请帮助我。提前致谢。
将您的 OnActivityResult
代码更改为:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_CANCELED) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
File fil = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : fil.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
fil = temp;
break;
}
}
Bitmap bitmap = null;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
Bitmap bmp = BitmapFactory.decodeFile(fil.getAbsolutePath(),bitmapOptions);
//
Log.e("edit", "new image path is " + fil.getAbsolutePath());
} catch (Exception e) {
// Log.i(TAG, "Exception:" + e.getMessage());
}
ShowPopupDialog(mCurrentPhotoPath);
}
}
我在 onActivityResult() 中收到 "photoFile" is null
单击捕获按钮时,我调用了一个方法 dispatchTakePictureIntent() 但是当我 return 返回时它得到空值。
当图像分辨率/尺寸非常大(如 4012*4012 等)时会出现此问题,特别是在 Android 6.0 版 +
上会出现此问题这是我的代码
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(
android.provider.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 = Util.createImageFile();
mCurrentPhotoPath = photoFile.getAbsolutePath();
System.out.println( "photoFile:" + photoFile.getAbsolutePath());
} catch (IOException ex) {
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
System.out.println("@@@@ photoFile: " + photoFile.getAbsolutePath());
/*takePictureIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT,1024);
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
mCurrentPhotoPath);*/
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_CANCELED) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
//Bundle extras = data.getExtras();
// Log.i(TAG, "extras: " + extras.get("data"));
Bitmap mphoto = (Bitmap) data.getExtras().get("data");
System.out.println( "***photoFile: " + data.getExtras().get("data"));
} catch (Exception e) {
// Log.i(TAG, "Exception:" + e.getMessage());
}
ShowPopupDialog(mCurrentPhotoPath);
}
}
}
请帮助我。提前致谢。
将您的 OnActivityResult
代码更改为:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_CANCELED) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
File fil = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : fil.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
fil = temp;
break;
}
}
Bitmap bitmap = null;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
Bitmap bmp = BitmapFactory.decodeFile(fil.getAbsolutePath(),bitmapOptions);
//
Log.e("edit", "new image path is " + fil.getAbsolutePath());
} catch (Exception e) {
// Log.i(TAG, "Exception:" + e.getMessage());
}
ShowPopupDialog(mCurrentPhotoPath);
}
}