Can't take photo from camera only in Lenovo A750 - android

I try take photo from camera this way:
private void photo() {
String storageState = Environment.getExternalStorageState();
if (storageState.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String directory = Environment.getExternalStorageDirectory()
.getName() + File.separatorChar + "app/photo/";
mPushFilePath = directory + System.currentTimeMillis() + ".jpg";
File imageFile = new File(mPushFilePath);
Uri mImageFileUri = Uri.fromFile(imageFile);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
mImageFileUri);
startActivityForResult(intent, CAMERA_RESULT);
}
}
And I take picture in method onActivityResult from path mPushFilePath
In all devices this code working perfectly. But in Lenovo A750, when I try get picture, button v (or ok) doesn't work. Please, help me to fix this.

I solved the problem this way: When need to call the camera, if the device Lenovo, I call the custom camera that made ​​myself. If not, everything is as it used to.

Related

Retrieving camera intent image full image uri

I've found ways that have supposedly worked for people to retrieve the uri/bitmap of the camera taken image.
But when I try and create a bitmap out of the non-null uri that I'm getting the file isn't created and doesn't exist.
this the the function which I use to open the camera intent(Which is copied from stackoverflow):
private void openBackCamera() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
currentImageBase64 = storageDir.getAbsolutePath() + "/" + imageFileName;
File file = new File(currentImageBase64);
Log.d("2","file 1: " +file.exists());
Uri outputFileUri = FileProvider.getUriForFile(getApplicationContext(),"com.mydomain.fileprovider",file);
currentImageBase64 =outputFileUri+"";
Log.d("2",outputFileUri+" is the uri got from camera");
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
and this is how I handel my images:
Log.d("2","camera request!"+ currentImageBase64);
File imgFile = new File(currentImageBase64);
if(imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ivPreview.setImageBitmap(myBitmap);
}else{
Log.d("2","file does not exist");
}
note that the camera request log shows that currentImagesBase64 is not null and has a value "content://com.mydomain.fileprovider/name/Pictures/20190121_173332.jpg"
the file anyway, does not exist.
/**
edit: the problem was insufficent permissions, camera and read&write permissions were requested and the problem was resolved!
**/
Turn out Permissions were the problem, once I gave runtime camera read&write storage permissions the problem was fixed!

What could cause file path NULL only on Galaxy S4 5.0.1?

A file is being caught from the camera. We don't use Intent, but save a file into tempfile object. This is the code
private static final int IMAGE_CAPTURE = 1002; //class-wide
private String mCurrentPhotoPath; //class-wide
//...
File photoFile = createImageFile();
Uri uri = Uri.fromFile(photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, IMAGE_CAPTURE);
//...
private File createImageFile() throws IOException {
imageName = mCodeContent + "_" + Integer.toString(randomNumber) + ".png";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageName, ".png", storageDir);
mCurrentPhotoPath = image.getAbsoluteFile().getPath();
mImageNameList.add(imageName);
return image;
}
But this moment, a variable mCurrentPhotoPath is not a null.
However, after a user takes an image, the variable mCurrentPhotoPath becomes NULL in the onActivityResult so this creates a crash
case IMAGE_CAPTURE:
try {
Uri uri = Uri.fromFile(new File(mCurrentPhotoPath)); //<---CRASH!!!
handleCameraImage(uri);
} catch (Exception e) {
Toast.makeText(this, "ERROR (IMAGE_CAPTURE)" + "\nmCurrentPhotoPath=" +
mCurrentPhotoPath + "\n Log: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
break;
The image path on S4 looks like /storage/emulated/0/pictures/imageName.png.
What is causing or deleting mCurrentPhotoPath? And only on S4, not on other devices.
PS. I tried saving this value into the shared preferences, but at some point they get deleted as well.
In Samsung phones the orientation will change when camera app is opened. This will cause our activity to restart.
So add configchanges to that activity in manifest file
android:configChanges="orientation"
For guys in a similar situation. After getting this device I saw the issue.
Namely, S4 built-in camera forces landscape mode. If your app is locked to portrait like ours, then this will create an extra rotation which adds in an extra life cycle change.
onSaveInstanceState() will not help you here, but you need to implement onRestoreInstanceState() and initialize your objects there as well.

Android camera intent should not allow to save the image to gallery and store it in specified path

when i use this code ->
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
I am able to save the image to specified path but it is also saving to the gallery. I dont want to save the image to gallery. Please help here.
Thanks in advance for your valuable time.
try this
private void captureCameraImage() {
Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, getFilename());
startActivityForResult(chooserIntent, CAMERA_PHOTO);
}
method to return file name that you can specify whre you want to save
public String getFilename() {
File file = new File(Environment.getExternalStorageDirectory().getPath(), "MyFolder/Images");
if (!file.exists()) {
file.mkdirs();
}
String uriSting = (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg");
return uriSting;
}
Capture Image using below Intent -
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
// create a file to save the image
File MyDir = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir");
if (!MyDir.exists()){
MyDir.mkdirs()
}
File fileUri = new File(MyDir.getAbsolutePath() + File.separator + "IMG_"+ timeStamp + ".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
The gallery app scans the folders for Media contents and populates them in gallery.
If you wish not to display your captured images in gallery follow below methods-
Creating A .Nomedia File
Adding A Dot Prefix
I faced the same problem and implemented several workarounds similar to this one.
I tried also to keep the file hidden adding the . prefix to the filname and to put a .nomedia file (see MediaStore.MEDIA_IGNORE_FILENAME) within the folder where I stored the images but in some cases, calling the camera app via intent as usual
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
Uri fileUri = FileProvider.getUriForFile(getContext(), getString(R.string.file_provider_authority), file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
} else {
showToastMessage(getString(R.string.no_camera_activity), Toast.LENGTH_LONG);
}
depending on the device and the camera app, this latter might store the picture also within the gallery (usually saving a file with a timestamp as the filename) even if you are providing an Uri associated to a file you are storing within your application private partition.
So I found that the most reliable way of doing what I needed was to control the camera directly by your own or to adopt cwac-cam2 library provided by CommonsWare within YourActivity in this way (note the commented .updateMediaStore() line)
Uri fileUri = FileProvider.getUriForFile(getContext(), getString(R.string.file_provider_authority), file);
CameraActivity.IntentBuilder builder = new CameraActivity.IntentBuilder(this); // this refers to the activity instance
Intent intent = builder
.skipConfirm()
.facing(Facing.BACK)
.to(fileUri)
//.updateMediaStore() // uncomment only if you want to update MediaStore
.flashMode(FlashMode.AUTO)
.build();
startActivityForResult(intent, CAMERA_REQUEST_CODE);

Images not being saved when picture is taken by camera app that isn't the stock camera

I'm currently trying to save images taken from a phone to its gallery, but the code below only works if I choose the stock camera app when the chooser dialog pops up. Whenever I choose another camera app(e.g., the Google camera), the taken picture doesn't get saved any where.
To make things even stranger, sometimes the picture does show up in its designated directory in the gallery, but after 15 mins or so, the same goes for when I use the stock camera app: the picture will get saved in the default camera shots directory, but takes quite a bit to show up in its designated directory, if it shows up there at all.
// Capturing Camera Image will launch camera app request image capture
void captureImage() {
//file uri to store image.
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
// Request camera app to capture image
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
getActivity().startActivityForResult(captureIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
well ,
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
does not work anymore .
you should do something like this :
call Camera Activity :
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
and onActivityResult :
if (data.getData() == null) {
Bitmap bm = (Bitmap)
data.getExtras().get("data");
String timeStamp = new SimpleDateFormat(
"yyyyMMdd_HHmmss").format(new Date());
File pictureFile = new File(Environment
.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
.getAbsolutePath()
+ File.separator + "IMG_" + timeStamp);
try {
FileOutputStream fos = new FileOutputStream(
pictureFile);
bm.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();
String filePath = pictureFile.getAbsolutePath();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } else {
Uri imgUri =data.getData());
}
It turns out my code was working after all. The pictures were being saved in the new directory, but the problem was that the gallery wasn't being updated, which explains why the photos would randomly appear in the directory later on. Being new to this, it never occurred to me that I would have to update the gallery. I only came to this realization after using ES File Explorer to look through my files. To fix my problem, I just made a new method in my CameraFragment that would call on the media scanner. I called this method from onActivityResult().
Here's the new method, though there's nothing really "new" about it since I ran into the same code on other SO questions:
protected void mediaScan() {
getActivity().sendBroadcast(
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse(fileUri.toString())));
}
I also don't need to call the package manager and iterate through the apps that could handle the camera intent if I'm not giving the option to use choose a picture from a gallery, so I'm going to remove all that from my question.

Default Camera Activity Not Finishing Upon OK button press

I'm calling the default camera from my activity and then handling the onActivityResult. My code seems to work fine on the LG Ally which doesn't have a confirmation when a picture is taken. However, when I run the same app on the Nexus S, it prompts me with an "Ok", "Retake", or "Cancel" before returning to my activity. While "Cancel" works, returning to my activity without saving the picture, "Ok" doesn't seem to have any effect, not even returning to my activity.
My code below:
private void captureImage() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/" + (new UserContextAdapter(this)).getUser() + "/");
path.mkdirs();
File file = new File(path, "Image_Story_" + mRowId.toString() + ".jpg");
newImageUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, newImageUri);
startActivityForResult(intent, CAPTURE_IMAGE);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case CAPTURE_IMAGE:
switch (resultCode ) {
case 0:
Log.i("CAPTURE", "Cancelled by User");
break;
case -1:
mImageUri = newImageUri;
setImageFromUri();
}
}
I think I just had the exact same problem.
If the path to save the picture isn't correct, the camera won't return to your app. Once I made sure the directory exists, everything worked fine. Make sure the directory exists, then it should work.
-- Edit --
I just saw, that you call path.mkdirs(); but I think that it doesn't work. As you can read in the android doc "Note that this method does not throw IOException on failure. Callers must check the return value.". Please be sure to check if the directory really exists.
hth
Also, make sure your application has <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> if you're using Environment.getExternalStorageDirectory().getPath() above.
Hope this helps =)
please check this
Case 1:
Uri newImageUri = null;
File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/");
path.mkdirs();
boolean setWritable = false;
setWritable = path.setWritable(true, false);
File file = new File(path, "Image_Story_" + System.currentTimeMillis() + ".jpg");
newImageUri = Uri.fromFile(file);
Log.i("MainActivity", "new image uri to string is " + newImageUri.toString());
Log.i("MainActivity", "new image path is " + newImageUri.getPath());
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, newImageUri);
startActivityForResult(intent, REQUEST_CODE_CAPTURE_IMAGE);
Case 2:
String fileName = "" + System.currentTimeMillis() + ".jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
Uri imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Log.i("MainActivity", "new image uri to string is " + imageUri.toString());
Log.i("MainActivity", "new image path is " + imageUri.getPath());
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, REQUEST_CODE_CAPTURE_IMAGE);
I am able to save images through camera on nexus s in both of the above cases
In case 1:
a.Image is stored in custom folder.
b. If the “System.currentTimeMillis()” is changed to (“new Date().toString()”) image is not saved and camera does not return to my activity.
(Probably because “System.currentTimeMillis” has no spaces and “new Date().toString()” might be having some special characters and spaces)
In case 2:
a. Image is stored in camera folder
Thanks to all
I have same problem.You have to do only one job,in your Phone storage check that you have Pictures directory or not.If you don't have such library then make it manually.
Hope this works for you.

Categories

Resources