我进行了很多搜索,但没有任何帮助,所以我发布了自己的问题。
我的问题是在Android棉花糖及更高版本中拍照时,这给了我错误
java.lang.NullPointerException:uri
注意:在Api级别22以下,它可以正常工作
我的代码是
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_CANCELED) {
return;
}
if (requestCode == GALLERY) {
filePath = data.getData();
Uri uri = data.getData();
// imageView.setImageURI(uri);
try {
Log.d("waqarr",""+ Arrays.toString(convert(getPath(filePath))));
File f = new File(getPath(filePath));
file_name = f.getName();
Uri selectedImage = data.getData();
//ShowImageDialog();
if(filePath!= null)
{
multipart();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} else if (requestCode == CAMERA) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
// filePath = getImageUri(getApplicationContext(), photo);
filePath = data.getData();
try {
Log.d("waqarr", " " + Arrays.toString(convert(getPath(filePath))));
File f = new File(getPath(filePath));
file_name = f.getName();
multipart();
} catch (IOException e) {
e.printStackTrace();
}
}} public String getPath(Uri uri)
{
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
cursor.close();
cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
return path;
}
任何帮助将不胜感激。
答案 0 :(得分:0)
在片段/活动中添加以下代码,以从相机或图库中选择图像。这是您的相机或图库请求的int值
public static final int MY_PERMISSIONS_REQUEST_CAMERA = 001;
public static final int MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE = 002;
android的用户相机类,用于设置相机图像的外观和固定大小
Camera camera;
camera = new Camera.Builder()
.resetToCorrectOrientation(true) // it will rotate the camera bitmap to the correct orientation from meta data
.setTakePhotoRequestCode(1)
.setDirectory("pics")
.setName("PicName_" + System.currentTimeMillis())
.setImageFormat(Camera.IMAGE_JPEG)
.setCompression(75)
.setImageHeight(1000) // it will try to achieve this height as close as possible maintaining the aspect ratio;
.build(this);
检查所需权限
if (ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity)
getContext(), Manifest.permission.CAMERA)) {
} else {
ActivityCompat.requestPermissions((Activity) getContext(),
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
}
if (ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity)
getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions((Activity) getContext(),
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE);
}
}
从图库中选择图片
/* Choose an image from Gallery */
void openImageChooser() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PICTURE);
}
从相机拍照
textCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
camera.takePicture();
}catch (Exception e){
e.printStackTrace();
}
dialog.dismiss();
}
});
在活动结果上,根据您的选择以位图/ base64的形式获取所选图像
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PICTURE && resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.PNG, 40, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
Log.e("byte_array ", byteArray.toString());
imgBase = Base64.encode(byteArray);
imagePath = "data:image/png;base64," + imgBase;
Log.e("path_of_gallery", imgBase.toString());
// Set the image in ImageView
imgUserProfilePicture.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
Bitmap bitmap = camera.getCameraBitmap();
if(bitmap != null) {
imgUserProfilePicture.setImageBitmap(bitmap);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
Log.e("byte_array ", byteArray.toString());
imgBase = Base64.encode(byteArray);
imagePath = "data:image/png;base64," + imgBase;
Log.e("path_of_camera_img", imgBase.toString());
}else{
Toast.makeText(context,"Picture not taken!",Toast.LENGTH_SHORT).show();
}
}
}
答案 1 :(得分:-1)
首先为uri创建对象,然后您将不会获得空指针异常并对其进行调试