CODE
mCameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int permissionCheck = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
startCameraActivity();
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(cameraIntent, 1000);
}
}
else {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, 1000);
}
}
});
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==1000 && grantResults.length > 0){
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(cameraIntent, 1000);
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 1000) {
first_getUri = data.getData();
Bitmap bitmap = null;
try {
bitmap = getBitmapFromUri(first_getUri); //Here is problem
} catch (IOException e) {
Toast.makeText(getContext(), getString(R.string.failed_in_creating_bitmap_from_uri), Toast.LENGTH_LONG).show();
}
File imageFile = null;
try {
imageFile = createFileFromBitmap(bitmap);
} catch (IOException e) {
Toast.makeText(getContext(), getString(R.string.failed_in_creating_file_from_bitmap), Toast.LENGTH_LONG).show();
}
returnUri = Uri.fromFile(imageFile);
}
Glide.with(this)
.load(returnUri)
.override(1280, 1280)
.into(mImageview);
}
}
在此代码中,打开相机效果不错,但是当我拍照并进入时,
显示错误。
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=66536, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.keepair.www.pinair/com.keepair.www.pinair.MainActivity}: java.lang.NullPointerException: uri
Caused by: java.lang.NullPointerException: uri
onActivityResult的uri部分 - bitmap = getBitmapFromUri(first_getUri);
我想我无法从CameraActivity获得uri。
如何才能获得正确的uri图片?
请你教我吗?
修改
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
ParcelFileDescriptor parcelFileDescriptor =
getActivity().getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fileDescriptor, null, opts);
int width = opts.outWidth;
int height = opts.outHeight;
float sampleRatio = getSampleRatio(width, height);
opts.inJustDecodeBounds = false;
opts.inSampleSize = (int) sampleRatio;
Bitmap resizedBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, opts);
Log.d("Resizing", "Resized Width / Height : " + resizedBitmap.getWidth() + "/" + resizedBitmap.getHeight());
parcelFileDescriptor.close();
return resizedBitmap;
}
private float getSampleRatio(int width, int height) {
final int targetWidth = 1280;
final int targetheight = 1280;
float ratio;
if (width > height) {
// Landscape
if (width > targetWidth) {
ratio = (float) width / (float) targetWidth;
} else ratio = 1f;
} else {
// Portrait
if (height > targetheight) {
ratio = (float) height / (float) targetheight;
} else ratio = 1f;
}
return Math.round(ratio);
}
private File createFileFromBitmap(Bitmap bitmap) throws IOException {
File newFile = new File(getActivity().getFilesDir(), makeImageFileName());
FileOutputStream fileOutputStream = new FileOutputStream(newFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.close();
return newFile;
}
private String makeImageFileName() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_hhmmss");
Date date = new Date();
String strDate = simpleDateFormat.format(date);
return strDate + ".png";
}
崩溃日志
FATAL EXCEPTION: main
Process: com.keepair.www.pinair, PID: 4827
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=263144, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.keepair.www.pinair/com.keepair.www.pinair.MainActivity}: java.lang.NullPointerException: uri
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: uri
at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:60)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:922)
at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:795)
at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:749)
at com.keepair.www.pinair.GreenFragment.getBitmapFromUri(GreenFragment.java:385)
at com.keepair.www.pinair.GreenFragment.onActivityResult(GreenFragment.java:357)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:165)
at android.app.Activity.dispatchActivityResult(Activity.java:6428)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:2)
此代码也在6.0和7.0上进行了测试
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
private static final int CROP_FROM_CAMERA = 2;
private static final int PICK_FROM_FILE = 3;
public static final int camPermissionRequestCode=6;
public static final int galleryPermissionRequestCode=4;
private Uri imageUri;
private File profilePicPicked;
@Override
public void onClick(View v) {
int id=v.getId();
switch (id){
case R.id.camera:
if (Build.VERSION.SDK_INT >= 23) {
//do your check here
isStoragePermissionGranted(camPermissionRequestCode);
} else {
captureImage();
}
break;
case R.id.upload_image:
if (Build.VERSION.SDK_INT >= 23) {
//do your check here
isStoragePermissionGranted(galleryPermissionRequestCode);
} else {
pickFromGallery();
}
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode != Activity.RESULT_OK)
return;
switch (requestCode) {
case CAMERA_CAPTURE_IMAGE_REQUEST_CODE:
/**
* After taking a picture, do the crop
*/
File file2 = handleImageUri(imageUri,getContext());
try {
ExifInterface ei = new ExifInterface(file2.getAbsolutePath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
file2=rotateImage(file2, 90);
/* Toast.makeText(getActivity(),"90",Toast.LENGTH_SHORT).show();*/
break;
case ExifInterface.ORIENTATION_ROTATE_180:
file2=rotateImage(file2, 180);
/* Toast.makeText(getActivity(),"180",Toast.LENGTH_SHORT).show();*/
break;
case ExifInterface.ORIENTATION_ROTATE_270:
file2=rotateImage(file2, 270);
/* Toast.makeText(getActivity(),"270",Toast.LENGTH_SHORT).show();*/
break;
case ExifInterface.ORIENTATION_NORMAL:
/*Toast.makeText(getActivity(),"default",Toast.LENGTH_SHORT).show();*/
default:
break;
}
}catch (Exception e){
e.printStackTrace();
}
profilePicPicked= file2;
break;
case PICK_FROM_FILE:
/**
* After selecting image from files, save the selected path
*/
/*Toast.makeText(getContext(), "Image Taken", Toast.LENGTH_LONG).show();*/
imageUri = data.getData();
//iv_post.setImageURI(fileUri);
File fileGAllr = getRealPathFromURIForGallery(getActivity(),imageUri);
profilePicPicked=fileGAllr;
// new Updata().execute("");
// btn_gallery.setVisibility(View.GONE);
break;
case CROP_FROM_CAMERA:
break;
}
}
public static File getRealPathFromURIForGallery(Context context, Uri uri) {
File file = null;
try {
String extension="";
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
extension= filePath.substring(filePath.lastIndexOf("."));
System.out.print("");
}
cursor.close();
InputStream input = null;
input = context.getContentResolver().openInputStream(uri);
if (input == null) {
return null;
}
file = new File(context.getCacheDir(), System.currentTimeMillis()+"_Photo"+extension);
OutputStream output = new FileOutputStream(file);
try {
byte[] buffer = new byte[4 * 1024];
int read;
while ((read = input.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
} finally {
output.close();
input.close();
}
} catch (IOException e) {
Log.d("Error", e.toString());
}
return file;
}
public File rotateImage(File f, float angle) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap source = BitmapFactory.decodeFile(f.getAbsolutePath(), options);
Matrix matrix = new Matrix();
matrix.postRotate(angle);
Bitmap finalBm= Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
File editedFile=getOutputMediaFile(MEDIA_TYPE_IMAGE);
FileOutputStream out = null;
try {
out = new FileOutputStream(editedFile);
finalBm.compress(Bitmap.CompressFormat.JPEG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
f.delete();
return editedFile;
}
public static File handleImageUri(Uri uri, Context context) {
Pattern pattern = Pattern.compile("(content://media/.*\\d)");
Matcher matcher = pattern.matcher(uri.getPath());
if (matcher.find()) {
return getRealPathFromURI(context, uri);
} else {
return new File(uri.getPath());
}
}
public static File getRealPathFromURI(Context context, Uri uri) {
File file = null;
try {
InputStream input = null;
input = context.getContentResolver().openInputStream(uri);
if (input == null) {
return null;
}
file = new File(context.getCacheDir(), System.currentTimeMillis()+"Photo.png");
OutputStream output = new FileOutputStream(file);
try {
byte[] buffer = new byte[4 * 1024];
int read;
while ((read = input.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
} finally {
output.close();
input.close();
}
} catch (IOException e) {
Log.d("Error", e.toString());
}
return file;
}
public boolean isStoragePermissionGranted(int requsetCode) {
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(getActivity(),android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
if(requsetCode==camPermissionRequestCode){
captureImage();
}else{
pickFromGallery();
}
return true;
} else {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requsetCode);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
if(requsetCode==camPermissionRequestCode){
captureImage();
}else{
pickFromGallery();
}
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
if(requestCode==camPermissionRequestCode){
captureImage();
}else if(requestCode==galleryPermissionRequestCode){
pickFromGallery();
}
//resume tasks needing this permission
}
}
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
private void pickFromGallery(){
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_FROM_FILE);
}
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
private File getOutputMediaFile(int type) {
// TODO Auto-generated method stub
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"marcopolo");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
答案 1 :(得分:1)
一般来说,主要的矛盾是某些手机上的data.getData()
会返回null。
所以试试这个
添加你的片段
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.container, new TestFragment(), "cam");
fragmentTransaction.commit();
片段中的
// you can config a path to save photo and put this path to intent
// if you don't config a path, you can not get the uri path
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
getActivity().startActivityForResult(cameraIntent, 1000);
}
on Activity#onActivityResult()
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
getSupportFragmentManager().findFragmentByTag("tag").onActivityResult(requestCode, resultCode, data);
}
on fragment#onActivityResult()
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1000) {
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.get("data");
}
}
super.onActivityResult(requestCode, resultCode, data);
}
答案 2 :(得分:1)
mCameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
openCameraMethod();//write the code to open camera
}else checkForPermission();
}
});
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
openCameraMethod();//write the code to open camera
} else {
// Permission Denied
Toast.makeText(MainActivity.this, "Camera permission is Denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void checkForPermission() {
int hasWriteContactsPermission = ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CAMERA);
if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
showMessageOKCancel("You need to allow access Camera",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_ASK_PERMISSIONS);
}
});
return;
}
ActivityCompat.requestPermissions(MainActivity.this,
new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_ASK_PERMISSIONS);
return;
}
openCameraMethod();//write the code to open camera
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
//same code what ever u wnat to do
}