这是打开相机的方法。我不知道这个错误的specyfic开始,有时我得到这个错误,有时没有。我不知道为什么我的愚蠢队友使用意大利面条代码从相机获取图像,但我需要解决它。
那么为什么我会考虑添加其他resultCode来处理错误的东西 你们有什么想法?
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_TAKE_PHOTO) {
Uri imageUri = Uri.parse(photoAbsolutePath);
onPhotoResult(imageUri);
} else if (requestCode == REQUEST_OPEN_GALLERY) {
Uri imageUri = data.getData();
onPhotoResult(imageUri);
}
}
}
protected void onPhotoResult(Uri uriPath) {
Bitmap bitmap = Tools.loadBitmapFromGallery(uriPath, getActivity());
if (bitmap != null) {
float angle = Tools.getRotation(uriPath, getActivity());
if (angle != 0f) {
bitmap = Tools.rotate(bitmap, angle);
}
RequestUploadPhotos requestUploadPhotos = new RequestUploadPhotos();
byte[] photo = new Tools().getImageData(bitmap);
try {
File tempFile = File.createTempFile("temp", ".jpg");
if (tempFile != null) {
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(photo);
fos.close();
requestUploadPhotos.photoFilesList.add(tempFile);
}
} catch (IOException e) {
e.printStackTrace();
}
if (requestUploadPhotos.photoFilesList != null) {
UploadPhoto uploadPhoto = new UploadPhoto();
uploadPhoto.execute(requestUploadPhotos)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(response -> {
if (response.getSuccess()) {
List<ImageUrl> addedPhotos = new ArrayList<>();
for (int i = 0; i < response.newGalleryPhotos.size(); i++) {
addedPhotos.add(new ImageUrl(
response.newGalleryPhotos.get(i).getImageId(),
response.newGalleryPhotos.get(i).getImageUrl()));
}
myGalleryAdapter.addPhotosToGallery(addedPhotos);
} else {
Toast.makeText(getContext(), response.getActionRes(), Toast.LENGTH_SHORT).show();
}
}, throwable -> throwable.printStackTrace(), () -> {
});
} else {
Toast.makeText(getContext(), "Error forming request", Toast.LENGTH_SHORT).show();
}
}
}
java.lang.RuntimeException: Unable to resume activity {.NewMainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65536, result=-1, data=null} to activity {NewMainActivity}: java.lang.NullPointerException: uriString
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3176)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3207)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
at android.app.ActivityThread.access$900(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1378)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5665)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65536, result=-1, data=null} to activity .NewMainActivity}: java.lang.NullPointerException: uriString
at android.app.ActivityThread.deliverResults(ActivityThread.java:3778)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3163)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3207)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
at android.app.ActivityThread.access$900(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1378)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5665)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)
Caused by: java.lang.NullPointerException: uriString
at android.net.Uri$StringUri.<init>(Uri.java:477)
at android.net.Uri$StringUri.<init>(Uri.java:467)
at android.net.Uri.parse(Uri.java:439)
at co.spontime.ui.fragments.UserProfileFragment.onActivityResult(UserProfileFragment.java:307)
编辑:
@RequiresPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
private void dispatchTakePicture() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
Log.i("DispatchTakePicture", "file created:" + photoFile.getAbsolutePath());
} catch (IOException ex) {
// Error occurred while creating the File
Log.e(this.getClass().getName(), "Error, while creating a directory \n" +
ex.toString());
}
// Continue only if the File was successfully created
if (photoFile != null) {
try {
photoAbsolutePath = "file:" + photoFile.getAbsolutePath();
Uri photoUri = FileProvider.getUriForFile(getContext(),
"co.spontime.fileprovider", photoFile);
getContext().grantUriPermission("com.android.camera", photoUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
//you must setup this
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
} catch (IllegalArgumentException e) {
Log.e("TakePhoto", "Failed providing Uri from FileProvider");
e.printStackTrace();
}
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
答案 0 :(得分:0)
是的,因为您使用的onActivityResult方法 Uri imageUri = Uri.parse(photoAbsolutePath);其中photoAbsolutePath可能为null。
你应该从数据意图中得到uri。除此之外,你应该使用相同的请求代码,无论你在startActivityForResult时采取了什么。
答案 1 :(得分:0)
这是我用来在Android 8和Android 10(分别是Android Oreo和Android Q)上保存图片的javaCode:
// altura =高度; // largura = Width();
if (requestCode == CAMERA && data == null) {//Android 10 (Android Q > API 23)
String[] proj = {MediaStore.Images.Media.DATA};
// Cursor cursor = getActivity().getContentResolver().query(image_uri, proj, null, null, null);
Cursor cursor = Objects.requireNonNull(getActivity()).getContentResolver().query(image_uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String caminho = cursor.getString(column_index);//diretorio
cursor.close();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), image_uri);
int altura = bitmap.getHeight();//altura;
int largura = bitmap.getWidth();//largura
if (largura > 450) {
if (altura > largura) {//retrato
bitmap = getResizedBitmap(bitmap, 450, 600);
saveImage(bitmap);
}
if (largura > altura) {//paisagem
bitmap = getResizedBitmap(bitmap, 600, 450);
saveImage(bitmap);
}
if (largura == altura) {//quadrado
bitmap = getResizedBitmap(bitmap, 450, 450);
saveImage(bitmap);
}
apagaFotoOriginal(caminho);
}
salvarBD();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(context, "Falhou!", Toast.LENGTH_SHORT).show();
}
atualizarLista();
}
在我使用此行代码从我对Camera Intent要求的光标获取imageUri之前:
//游标游标= getActivity()。getContentResolver()。query(image_uri,proj,null,null,null);
在我为此选择之后:
//游标游标= Objects.requireNonNull(getActivity())。getContentResolver()。query(image_uri,proj,null,null,null);
CameraIntent: 私人无效abrirCamera(){ ContentValues值=新的ContentValues(); image_uri = getActivity()。getContentResolver()。insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,值);
//Camera intent
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
startActivityForResult(cameraIntent, CAMERA);
}
这和我一起工作,可以尝试。