Android : save/open picture taken from camera intent - android

I have a camera intent that takes a picture that I want to save anywhere as a temp file :
File photo = new File(Environment.getExternalStorageDirectory(), "myTempPicture.jpg");
imageUri = Uri.fromFile(photo);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent,0);
(Maybe the choice of Storage is not the right one)
Then after the picture was taken, I need to open it to process it :
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
process(bitmap, false);
}catch(Exception e){
Log.d("exception : ", e.toString());
}
}
But I get the following Exception : java.io.FileNotFoundException: /storage/emulated/0/savedImage.jpg (Permission denied).
What went wrong ?
My Manifest file contains :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

with android 6.0+ or SDK 23 and above you have to ask runtime permission in your activity
here an example how you can do that, here 1 is your request code
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
}
and then you should check if user granted permission
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (!(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
}
}
}
}
for more detailed guide about runtime request have a look here at official documentation

Related

Android: Camera Error - > Cannot connect to the camera

I have a problem with my app. Before this, if I want to use camera function, the camera will run well. But today, if I want to use the camera function, it states that "Cannot connect to the camera". I don't why. Below is my code:
btnCameraBefore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 7);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 7 && resultCode == RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imgAttachBefore.setImageBitmap(bitmap);
}
}
public void EnableRuntimePermission(){
if (ActivityCompat.shouldShowRequestPermissionRationale(TaskUpdateBefore.this,
Manifest.permission.CAMERA))
{
Toast.makeText(TaskUpdateBefore.this,"CAMERA permission allows us to Access CAMERA app", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(TaskUpdateBefore.this,new String[]{
Manifest.permission.CAMERA}, RequestPermissionCode);
}
}
#Override
public void onRequestPermissionsResult(int RC, String per[], int[] PResult) {
switch (RC) {
case RequestPermissionCode:
if (PResult.length > 0 && PResult[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(TaskUpdateBefore.this,"Permission Granted, Now your application can access CAMERA.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(TaskUpdateBefore.this,"Permission Canceled, Now your application cannot access CAMERA.", Toast.LENGTH_LONG).show();
}
break;
}
}
Manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
Can anyone help?

Not able to access camera even though proper permissions are declared in manifest file

I wrote a code to access camera to take photos for profile page. Code is as given below:
case R.id.textViewTakePhoto:
try {
Utility.showPopupMenuWindow(mContext, false, mPopupWindow);
int camera = ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.CAMERA);
if (camera != PackageManager.PERMISSION_GRANTED) {
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
else{
ActivityCompat.requestPermissions((Activity) mContext,
new String[]{android.Manifest.permission.CAMERA},
9);
}
} catch (Exception e) {
Log.e(TAG, "Can't access camera" + e.getMessage());
e.printStackTrace();
}
break;
In my manifest file, I've added the following code:
<uses-permission android:name="android.permission.CAMERA" />
<!--for camera purposes-->
<uses-feature
android:name="android.hardware.camera.any"
android:required="true" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
compileSdkVersion is 25. When I try to run the application, it throws the error:
Can't access cameraPermission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE cmp=com.google.android.GoogleCamera/com.android.camera.activity.CaptureActivity } from ProcessRecord{1d685bf 17561:com.dell.engage/u0a215} (pid=17561, uid=10215) with revoked permission android.permission.CAMERA
How can I fix this
You have used wrong code while checking permission is granted or not. See below code :
if (camera != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) mContext,
new String[]{android.Manifest.permission.CAMERA},9);
}
else{
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
Try this code :
in your manifest
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
Java code
in your onClick methode :
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, RESULT_LOAD_CAMERA);
and then implement onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RESULT_LOAD_CAMERA && resultCode == RESULT_OK && null != data) {
Bundle bundle = data.getExtras();
final Bitmap btm = (Bitmap) bundle.get("data");
mImgView.setImageBitmap(btm);
} else {
Toast.makeText(MainActivity.this, "no image selected", Toast.LENGTH_LONG).show();
}
}
You havent declared any permissions. Try something like this:
if(ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}else {
String[] permissionRequest = {Manifest.permission.CAMERA};
requestPermissions(permissionRequest, 8675309);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 8675309){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
} else {
Toast.makeText(EnterDataView.this, "Cannot take photo without permission.", Toast.LENGTH_LONG).show();
}
}
}
Also check if you have given CAMERA permission in your manifest file like this:
<uses-permission android:name="android.permission.CAMERA" />
Try this code:
#OnClick(R.id.button_camera)
void onClickCamera() {
mPresenter.openCamera(this);
}
#OnClick(R.id.gallery_button)
void onClickGallery() {
mPresenter.openGallery(this);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mPresenter.startEditing(requestCode, resultCode);
}
#Override
public void startGallery() {
Intent intent = new Intent(HomeActivity.this, GalleryActivity.class);
startActivity(intent);
}
#Override
public void startCamera(Uri uri) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, REQ_CAMERA);
}
}

Launch APK Installer Oreo

I have had this code working for some time now but with Android Orea it doesn't work.
Context context = mContextRef.get();
if (context == null) {
return;
}
// Get the update
String filepath = getDownloadLocation();
File apkFile = new File(filepath);
Uri fileLoc;
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
fileLoc = android.support.v4.content.FileProvider.getUriForFile(context, mFileProviderAuthority, apkFile);
} else {
intent = new Intent(Intent.ACTION_VIEW);
fileLoc = Uri.fromFile(apkFile);
}
intent.setDataAndType(fileLoc, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
This code works with all version of android except 8.0 and above.
Edit
Sorry for not explaining more. IT doesn't work meaning, it flashes a window for a fraction of a second and disappears (some times imperceptibly) and continues on in the old app. The file is successfully downloaded (I can navigate to it and install) but when I try to start the activity to install it programmatically it fails with no errors or logs at all.
Apparently if your app targets later versions of android the android.permission.REQUEST_INSTALL_PACKAGES permission is required in the manifest otherwise nothing happens except a single line error in the LogCat.
So include the following in manifest.xml file:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
Manifest API for REQUEST_INSTALL_PACKAGES
Please add the <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/> permission in Manifest.xml first.
We should check the result of getPackageManager().canRequestPackageInstalls() if the SDK version is equal or large than 26.
The code is below:
private void checkIsAndroidO() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
boolean result = getPackageManager().canRequestPackageInstalls();
if (result) {
installApk();
} else {
// request the permission
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.REQUEST_INSTALL_PACKAGES}, INSTALL_PACKAGES_REQUESTCODE);
}
} else {
installApk();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case INSTALL_PACKAGES_REQUESTCODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
installApk();
} else {
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
startActivityForResult(intent, GET_UNKNOWN_APP_SOURCES);
}
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case GET_UNKNOWN_APP_SOURCES:
checkIsAndroidO();
break;
default:
break;
}
}
I hope it will help you.

Not able to place a image in ImageView After taking a pic from camera only in Pixel_Api_26 emulator

I am trying to take a pic from cam in my app, but it is working fine in but not able to display it in imageview it's throwing error..
please help me
my code is
It is giving error at the time of Decoding bitmapUri (i mentioned in code)
the error is:
private void onCaptureImageResult(Intent data) {
Log.w("CAM","capture image result");
launchMediaScanIntent();
try {
File photo = new File(Environment.getExternalStorageDirectory(), "picture.jpg");
Log.w("CAM","decoding bitmap uri");
Context context=FSE_login.this;
Bitmap bitmap = decodeBitmapUri(this, FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".lerainfotech.tezmoney.FSE_login", photo));
Log.w("CAM","decoded");
attendance_pic = Utils.getResizedBitmap(bitmap);
Log.w("CAM","attached");
attendance_pic_field.setImageBitmap(attendance_pic);
} catch (Exception e) {
Log.w("Error Is",e);
Utils.showErrorDialog(getApplicationContext(), "Couldn't load image");
return;
}
}
First you need to write below code in Mainifest
<uses-permission-sdk-23 android:name="android.permission.CAMERA" />
<uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Your Activity should like this
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
}else{
captureImage();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == 0) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
captureImage();
}
}
}
private void captureImage(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "picture.jpg");
imageUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, REQUEST_CAMERA);
}

open failed: EACCES (Permission denied) in marshmallow while upload image to server

Im working on the project which i want to send image to server. When i send image to server all versions except marshmallow works fine.
I got the permission while pick the image from gallery. Is there is any I any special permission to send image ?
private static int PICK_IMAGE_REQUEST = 1;
if (Build.VERSION.SDK_INT <= 19) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_IMAGE_REQUEST);
} else if (Build.VERSION.SDK_INT > 19) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_IMAGE_REQUEST);
}
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
IN Activity
private static final int REQUEST_STORAGE = 112;
if (Build.VERSION.SDK_INT >= 23) {
String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE};
if (!hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST_STORAGE );
} else {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_IMAGE_REQUEST);
}
} else {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_IMAGE_REQUEST);
}
/*get Permissions Result*/
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_IMAGE_REQUEST);
} else {
Toast.makeText(mContext, "The app was not allowed to write to your storage.", Toast.LENGTH_LONG).show();
}
}
}
}
/*check permissions for marshmallow*/
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
You need to ask runtime permissions for user. Check this link https://developer.android.com/training/permissions/requesting.html
There is one more permission of Internet that might be missing to give.
"<uses-permission android:name="android.permission.INTERNET" />"
Also do go to settings and check if the permission has been granted as in marshmallow the permission is needed to be given at runtime..
Check below link....
https://developer.android.com/training/permissions/requesting.html
Hope your problem gets resolved with this...

Categories

Resources