Android: Camera Error - > Cannot connect to the camera - android

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?

Related

Android pick images from gallery not working

I am trying to choose an image from device's gallery, crop it and then display it in an imageView. After I click on an image in image selector, the app quits with logcat message showing V/FA: Inactivity, disconnecting from the service. I am running a service in background to get data from firestore.
I have also tried using cropping libraries like https://github.com/ArthurHub/Android-Image-Cropper but the behaviour was same.
I also tried the following.
FABfile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
});
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null){
mImageUri = data.getData();
imageView.setImageURI(result.getUri());
}
}
Please help to resolve this issue.
Use these methods to choose image from gallery:
public void showGallery() {
if (hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
loadGallery();
} else {
requestPermissionsSafely(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
}
private void loadGallery() {
Intent choose = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(choose, PICK_IMAGE_GALLERY);
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
loadGallery();
}
break;
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_GALLERY) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
}
}
}
And:
#TargetApi(Build.VERSION_CODES.M)
public void requestPermissionsSafely(String[] permissions, int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(permissions, requestCode);
}
}
#TargetApi(Build.VERSION_CODES.M)
public boolean hasPermission(String permission) {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
}

Android : save/open picture taken from camera intent

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

Video uploading on parse server android

I am a beginner in android, can someone provide me working tutorial on uploading a video from the mobile video gallery and saving it on parse server? Thanks in advance !
`Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
intent.setType("video/*");
startActivityForResult(intent, 1);
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull
String[]
permissions, #NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
if (requestCode == 1)
{
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED)
{
getPhoto();
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]
{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else {
getPhoto();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1 && resultCode==ChatActivity.RESULT_OK && data!=null)
{
try
{
VideoView mVideo=(VideoView) findViewById(R.id.videoView);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte[] byteArray =stream.toByteArray();
ParseFile file = new ParseFile("resume.mp4", byteArray);
ParseObject object = new ParseObject("Video");
object.put("video",file);
object.put("username",
ParseUser.getCurrentUser().getUsername());
object.saveInBackground(new SaveCallback()
{
#Override
public void done(ParseException e)
{
if(e==null)
{
Toast.makeText(ChatActivity.this,"Vidoe
Saved",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(ChatActivity.this,"Vidoe not
Saved",Toast.LENGTH_SHORT).show();
}
}
});
}
catch (Exception ex)
{
ex.printStackTrace();
}
}`
I have given permission in manifest file for accessing the external data too !
You most likely want to use the ParseFile as described here docs.parseplatform.org for your video.

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);
}
}

How do I make downloaded images fit to Image Button?

I am using the Android-Image-Cropper library. It seems to work fine with pictures taken with the camera itself, but when I select images from the gallery that were downloaded from the internet and crop them, the image does not completely cover the ImageButton.
Here is a picture of it not working (downloaded)
Here is one that is working (taken with camera)
Here is my code from the activity:
public void setProfilePicture(View view) {
CropImage.startPickImageActivity(this);
}
#Override
#SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// handle result of pick image chooser
if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri imageUri = CropImage.getPickImageResultUri(this, data);
// For API >= 23 we need to check specifically that we have permissions to read external storage.
if (CropImage.isReadExternalStoragePermissionsRequired(this, imageUri)) {
// request permissions and handle the result in onRequestPermissionsResult()
mCropImageUri = imageUri;
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE);
} else {
// no permissions required or already granted, can start crop image activity
startCropImageActivity(imageUri);
}
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
profilepicture.setImageURI(resultUri);
} else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception e = result.getError();
}
}
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (requestCode == CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE) {
if (mCropImageUri != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// required permissions granted, start crop image activity
startCropImageActivity(mCropImageUri);
} else {
Toast.makeText(this, "Cancelling, required permissions are not granted", Toast.LENGTH_LONG).show();
}
}
}
private void startCropImageActivity(Uri imageUri) {
CropImage.activity(imageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
}
}
Oh wait it figured it out.
I had to set android:scaleType="fitCenter" in my XML and
.setAspectRatio(1, 1)
.setFixAspectRatio(true)
to my startCropImageActivity method.

Categories

Resources