问题
I am trying to create and Android application that takes a photo or uploads a photo to the server. My main objective is to get the URI of the photo which is returned in the intent. I have followed the steps from [1]. The problem is that on a phone with Lollipop, version 5.1.1 it works fine and the intent returns the URI of the photo, but on a phone which has Jelly Bean, version 4.2.1, the URI returned in the intent is null.
Here is my code
Create View and set listeners to buttons
static final int REQUEST_TAKE_PHOTO = 1;
static final int REQUEST_GET_PHOTO = 2;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home_page, container, false);
camera = (FloatingActionButton) view.findViewById(R.id.take_photo);
upload = (FloatingActionButton) view.findViewById(R.id.upload_photo);
photo = (ImageView) view.findViewById(R.id.photo);
camera.setOnClickListener(takePhoto);
upload.setOnClickListener(uploadPhoto);
return view;
}
Take Picture Action
View.OnClickListener takePhoto = new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
};
Upload from gallery listener
View.OnClickListener uploadPhoto = new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
if (photoPickerIntent.resolveActivity(getActivity().getPackageManager()) != null) {
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, REQUEST_GET_PHOTO);
}
}
};
Handle Intent Result
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && (requestCode == REQUEST_TAKE_PHOTO || requestCode == REQUEST_GET_PHOTO)) {
Uri imageUri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getApplicationContext().getContentResolver(), imageUri);
String filePath = getPath(imageUri);
bitmap = BitmapRotator.rotateBitmap(bitmap, filePath);
photo.setImageBitmap(bitmap);
UploadPhotoController.uploadPhoto(filePath);
} catch (IOException e) {
e.printStackTrace();
} catch (PhotoNotFoundException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
data.getData() is null thus I can't get the uri, or the file path of the image.
Thank you.
[1] http://developer.android.com/training/camera/photobasics.html
回答1:
on StackOverflow you would find these topics according to yours:
My Android camera Uri is returning a null value, but the Samsung fix is in place, help?
Camera activity returning null android
Android Camera : data intent returns null
来源:https://stackoverflow.com/questions/34255825/uri-returned-in-camera-intent-is-null