I have created fragment with multiple image addition dialog. Each image can be added via camera or gallery. In both situations I can add only one image - after dismissing gallery/camera view application stops responding.
That's how I create camera and gallery pick intent:
//permissions request in other methods
private void pickImage() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_GALLERY);
}
private void openCamera() {
if (PermissionsHelper.checkStoragePermissions(ReportFragment.this)) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri fileUri = getOutputMediaFileUri();
fileFromCamera = fileUri.getPath();
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takePicture, SELECT_PHOTO);
} else {
PermissionsHelper.requestStoragePermissions(this, CAMERA_STORAGE_PERMISSION_REQUEST);
}
}
Here is how result handled:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == SELECT_GALLERY) {
Uri selectedImageAddress = data.getData();
addPathsIfUnique(getRealPathFromURI(selectedImageAddress));
}
if (requestCode == SELECT_PHOTO) {
if (PermissionsHelper.checkStoragePermissions(this)) {
if(fileFromCamera != null) {
addPathsIfUnique(fileFromCamera);
}
} else {
PermissionsHelper.requestStoragePermissions(this, CAMERA_STORAGE_PERMISSION_REQUEST);
}
}
}
private void addPathsIfUnique(String path) {
if(path == null)
return;
for(String currentPath : photoPaths) {
if(currentPath.equals(path)) {
return;
}
}
photoPaths.add(path);
shopChanged(point);
}
In the shopChanged function I'm recreating ListView cells. I was also trying to completely remove ListView, replace it with single button in fragment, but result was same: after second time opened gallery or camera closed, application becomes not responding. Even OnResume breakpoint not fired. But after first opening all works good.
What can be reason for this behaviour?
Found a solution: this is new Google Play Services bug - issue fixed after downgrade to 8.4.0. Will search they bugtracker and submit bug, if it isn't submitted already
Related
I have one activity and multiple fragments.Here my use case is I have to capture an image using from one of the fragment,the resultant image will shown in an ImageView in that fragment.But here the problem is the fragment destroying its view and activity was recreating again.How can I show the resultant image in that fragment ? This problem occuring only in RedmiNote4.Thanks in advance.
Here is my code
cameraBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//check permissions
if(hasPermission){
values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "Image File name");
mCapturedImageURI =getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
intentPicture.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if(intentPicture.resolveActivity(getActivity().getApplicationContext().getPackageManager()) != null) {
startActivityForResult(intentPicture,CAMERA_REQUEST);
}
}
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
return;
} else if(requestCode == CAMERA_REQUEST ){
Uri uri =mCapturedImageURI;
//setImage
loadImage(uri)
}
}
I am also RedmiNote4 user.
With this case I was facing that issue.
When I turn off flag "Don't keep activities" It solved my issue after digging solution for 2 days.
I'm new to Android developing and following the training course on google now, I meet a problem. After executing Bundle extra = data.getExtras(), extra is null. Also it will throw a nullpointerexception before I add the code "if(extra!=null)"
Here is the code of onActivityResult() method
Here is the code part to start the camera and save photo locally
All my code follows the training course and I am confused where is wrong.
Can someone help me?
Please update your onActivityResult(), the result from capture photo doesn't response an image. It returns a Uri that you passed in photo_intent.putExtra(MediaStore.EXTRA_OUTPUT, <<uri>>). So please declare the <<uri>> as global then you can get this value in onActivityResult()
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
hasWriteContactsPermission = checkSelfPermission(Manifest.permission.CAMERA);
if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
REQUEST_CODE_ASK_PERMISSIONS);
return;
}
} else {
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
File photo = new File(Environment
.getExternalStorageDirectory(),
"Journal" + Utils_class.getID() + ".jpeg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
mSelectedFilePath = photo.getAbsolutePath();
startActivityForResult(intent,
ACTION_TAKE_PICTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
try {
if (resultCode == RESULT_OK) {
if (requestCode == ACTION_TAKE_PICTURE) {
// flag = true;
// new Utils();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkPermissionForStorageForCam();
} else {
Uri selectedImageUri = data.getData();
}
}
}
} catch (Exception e) {
}
}
If you pass the extra parameter MediaStore.EXTRA_OUTPUT with the camera intent then camera activity will write the captured image to that path and it will not return the bitmap in the onActivityResult method.
If you will check the path which you are passing then you will know that actually camera had write the captured file in that path.
I'm switching from using the default camera to finally growing a pair and deciding to make a custom camera. It's only showing me how much I don't fully understand.
Here is basically way I have been doing things in the main activity as far as photos go, but it will no longer work:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == PICK_PHOTO_REQUEST) {
if (data == null) {
Toast.makeText(this, "General Error!", Toast.LENGTH_LONG).show();
}
else {
mMediaUri = data.getData();
}
Log.i(TAG, "Media URI: " + mMediaUri);
}
else {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(mMediaUri);
sendBroadcast(mediaScanIntent);
}
(...)
Here is the picture saving function along with a couple others of the new camera:
(...)
#Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("Picture taken");
String path = savePictureToFileSystem(data);
setResult(path);
finish();
}
private static String savePictureToFileSystem(byte[] data) {
File file = getOutputMediaFile();
saveToFile(data, file);
return file.getAbsolutePath();
}
private void setResult(String path) {
Intent intent = new Intent();
intent.putExtra(EXTRA_IMAGE_PATH, path);
setResult(RESULT_OK, intent);
}
*Credit to Paul Blundell
(...)
What do I need to do so that the main activity can receive the image's URI in the onactivityresult instead of the path String? Are URIs even applicable when it comes to custom cameras?
Please and thanks.
You use custom camera, but want to do it via Intent? OK, you have the absolute file path in
String abspath = data.getExtras().getString(EXTRA_IMAGE_PATH);
mMediaUri = Uri.fromFile(new File(abspath));
I am trying to upload image in my app and i want to show some choices when my upload button is clicked. Unfortunately it gives me this error. I dont know what is wrong with the code. I got this from hereTrying open a specific folder in android using intent
thanks in advance!
uploadpic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
openFolder();
}
});
}
public void openFolder()
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/Pictures/");
intent.setDataAndType(uri, "images");
startActivity(Intent.createChooser(intent, "Open folder"));
takephoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
dispatchTakePictureIntent();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
profpic.setImageBitmap(imageBitmap);
}
}
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
You only want the user to select an image, am I right? Then maybe a general file picker isn't whats best suited for your needs.
A quick google search came up with this (also from SO). It seems like that solution is limited to pictures on an SD card.
If you don't want to click the link:
This is how you would start an intent in order to get an image.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
You'll then need to override onActivityResult(), but this is all described in the link.
In my application, I'm trying to make it so the User can press a button, which will allow them to take a picture using the stock camera application on their phone.
I am following the guide to using an external Camera app to capture images that I can use in my own app from the Android Developers Guide (http://developer.android.com/guide/topics/media/camera.html#intent-receive)
I'm having trouble with the onActivityResult() method, it apparently takes in 3 parameters
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if(resultCode == RESULT_OK) {
Log.w("borre","Image saved to:\n" + data.getData());
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
But at the moment, the data Intent is coming back as null, so calling any methods on the Intent parameter throws a NullPointerException
Here's the code I'm using to call up the Camera application (It's basically the same as the code in the guide)
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
Has anyone had this problem or knows why this Intent is coming back as null?
your are getting data part null bez you are not setting intent.setDataAndType() when you are starting Acitivty.like
public static final String IMAGE_UNSPECIFIED = "image/*";
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
startActivityForResult(intent, 3);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 0)
return;
if (requestCode == 2) {
Uri uri=data.getData(); //YOU GET DATA HERE
}
//OR
if (requestCode == 3) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)????
imageView.setImageBitmap(photo);
}
}
or in your case getting image path use:
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
//pic path
File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
}