Ok, so I have looked around a lot for this but I can't seem to find anyone else that's had this problem.
I am making a simple application just to test out working with the camera. Everything works up until I actually press the button to capture the image. When I do, instead of freezing and asking for confirmation that this is the picture I want, the confirmation button come up at the bottom, but the live feed from the camera keeps going.
I am using the most basic way of sending an intent to ask android to take a picture so I don't know why this is happening.
Here is the code:
private static final int IMAGE_CAPTURE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addCaptureButtonListener();
}
private void addCaptureButtonListener() {
Button capture = (Button) findViewById(R.id.captureButton);
capture.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, IMAGE_CAPTURE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
if(requestCode == IMAGE_CAPTURE) {
Toast.makeText(this, "Image Successfully Taken", Toast.LENGTH_SHORT).show();
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
The camera feature depends vastly on the device you are using you may be using the correct method but sometimes it's the hardware issue. Also Please post the code, cross check if everything is fine from this example .
Capture Image from Camera and Display in Activity
try testing it on some other device if it works fine then look for the documentation of the bug for the device.
Edit
The problem is with the S4 it conflicts between the capture image resolution and the Preview image resolution the similar issues have been reported here too
Samsung Galaxy S4 , Image gets corrupted while taking from camera
Samsung galaxy S4 image capture Issue
your code is perfect it's just that the resolution camera clicks the image is not supported for the preview by the same device.
Related
I have had a problem for weeks and still can not solve, did a lot of research and tested many codes, but nothing solved. I will explain my problem in detail.
I am making an application where in a layout there would be three or more clickable photos, I am using the “ArthurHub / Android-Image-Cropper” image cropping library, the steps are:
Click on the image, which gives the option to open the image gallery to select a photo or take a new photo, after that I can crop
the selected image.
Select the second photo, do the same as the previous one, thereafter.
Upload the photos to the server.
Steps 1 is working correctly, the problem arises when I try to select from the second image.
What I'm using: I'm using two classes: the one that requests the images and the one that returns:
1. would be the main window with ImageButton.
2. and the other class that returns the selected images to the first class. (Contains the function that calls the gallery or camera, cuts the image, the other “onActivityResult” function that returns the image address, who asked for the picture).
So far everything works correctly.
Problem Description: The problem arises from the second image selected. when the second class returns everything to the first, it is as if the second image is stored in the same memory space as the first (erasing everything previously), and if I select the third image, it deletes the second and only remains the third and henceforth. What I want to do is select the images and have them all seen at the same time (usable) to send to a server.
Solutions I tried:
After days of searching, the suggestion was to make several returns on “onActivityResult” so that it returned the result of selecting
images separately, I couldn't find anything that worked, the
explanations I found were only halfway (including the official
documentation does not detail the steps to control the various returns
of the function, is very superficial), could not control the separate
pointing of the images.
switch (requestCode){
case (1000):
Intent intent1 = new Intent(TirarFoto1.this, RegistrarAutomovelDuasRodas.class);
intent1.putExtra("class", classname);
intent1.putExtra("imageview", imageview);
intent1.putExtra("pathimage", pathimage);
startActivity(intent1);
break;
case (2000):
Intent intent2 = new Intent(TirarFoto1.this, RegistrarAutomovelDuasRodas.class);
intent2.putExtra("class", classname);
intent2.putExtra("imageview", imageview);
intent2.putExtra("pathimage", pathimage);
startActivity(intent2);
case (3000):
Intent intent3 = new Intent(TirarFoto1.this, RegistrarAutomovelDuasRodas.class);
intent3.putExtra("class", classname);
intent3.putExtra("imageview", imageview);
intent3.putExtra("pathimage", pathimage);
startActivity(intent3);
break;
default: break;
}
I decided to save each image separately outside the “temporary cache” folder (where they are located), even with the “manifest”
permissions, nothing happens, permission is denied, so I couldn't even
create the new folder to save the images in it, as a result does not
save the selected images.
if (ContextCompat.checkSelfPermission(TirarFoto1.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
File file = new File(Environment.getExternalStoragePublicDirectory(String.valueOf(imageUri))+"folderName");
if (!file.exists()){
Toast.makeText(this, "exists", Toast.LENGTH_SHORT).show();
}
if (success){
Toast.makeText(this, "creaty", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "Erro!!!", Toast.LENGTH_SHORT).show();
}
}else {
requestStoragePermisson();
}
Result: The first solution did not work and the second did not work.
What I want: a model, a concrete way to help me, can anyone please help me with this? I have been stuck in this problem for a long time.
me to use ArthurHub-Android-Image-Cropper for cropped image, you can learn from this link
https://github.com/ArthurHub/Android-Image-Cropper/wiki
hope this help
Edit:
i have a class using ArthurHub-Android-Image-Cropper but from kotlin and i try to convert to java
like this
//for identify image
private Int imageNo;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//......
//set button image 1
final Button button1 = findViewById(R.id.button_image1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// set image count to 1
imageNo=1
getImageClick()
}
});
//set button image 2
final Button button1 = findViewById(R.id.button_image1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// set image count to 2
imageNo=2
getImageClick()
}
});
//set button image 3
final Button button1 = findViewById(R.id.button_image1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// set image count to 3
imageNo=3
getImageClick()
}
});
}
//select image form camera or galery
public void getImageClick() {
CropImage.startPickImageActivity(this);
}
//this for Crope Image
private void startCropImageActivity(Uri imageUri) {
CropImage.activity(imageUri)
.start(this);
}
#Override
#SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//handle result from onGetimageClick(button for select image from camera or galery)
if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == AppCompatActivity.RESULT_OK) {
Uri imageUri = CropImage.getPickImageResultUri(this, data);
//start Crope image
startCropImageActivity(imageUri);
}
// handle result of CropImageActivity
else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
Uri resultUri = result.getUri();
switch (imageNo){
case (1):
//here you have resultUri for save image or preview as image1
break;
case (2):
//here you have resultUri for save image or preview as image1
case (3):
//here you have resultUri for save image or preview as image1
}
}
}
don't forget to add manifest
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="#style/Theme.AppCompat"/>
Hope this help
I have implemented search view using Leanback Search Fragment in Android TV App.
It works fine for the keyboard input text. But the voice search returns RESULT_CLIENT_ERROR 99%. However the text appears in the edit text on the screen but does not return in onActivityResult. Once in a bluemoon I get RESULT_OK with correct data in the intent.
Device: Nexus Player
Even the default search works the same way most of the items. Hardly do i get the results via voice. (Search in home screen of nexus player i mean)
I try to get the text using the reference of the SearchEditText no luck but.
The following is the code:
setSpeechRecognitionCallback(new SpeechRecognitionCallback() {
#Override
public void recognizeSpeech() {
try {
startActivityForResult(getRecognizerIntent(), REQUEST_SPEECH);
} catch (final ActivityNotFoundException e) {
Log.d("ActivityNotFoundException",e.toString());
}
}
});
onActivityResult
#Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_SPEECH:
switch (resultCode) {
case Activity.RESULT_OK:
setSearchQuery(data, true);
break;
case RecognizerIntent.RESULT_CLIENT_ERROR:
Log.d("RESULT_CLIENT_ERROR", String.valueOf(requestCode));
break;
}
}
}
How do i make it work 100% Is there anything I can change in the intent provided?
This is a problem with Nexus player as even the default search operation of the Nexus Player behaves the same way.
However the same piece of code works 100% fine on Sony Bravia and other Android TVs.
I am trying to use the built-in camera application to take a photo and view it through an ImageView.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
addButtonListeners();
startCamera();
}
private void startCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, PHOTO_TAKEN);
}
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (requestCode == PHOTO_TAKEN) {
Bundle extras = intent.getExtras();
photo = (Bitmap) extras.get("data");
if (photo != null) {
ImageView image = (ImageView) findViewById(R.id.image_background);
image.setImageBitmap(photo);
} else {
Toast.makeText(this, R.string.unable_to_read_photo, Toast.LENGTH_LONG)
.show();
}
}
}
When holding the phone in portrait position this code works just fine, however when I take the picture in landscape it breaks, any ideas why or how to solve this?
Question in not defined with enough details to answer it for sure, but my guess would be the same as Shani Goriwal .
It looks like problems with configuration changes event - which happens each time orientation is changed (from landscape to portrait).
Try to add to AndroidManifest of your app following lines:
android:configChanges="orientation|screenSize"
(more details: http://developer.android.com/guide/topics/resources/runtime-changes.html)
I found a tutorial that explains how to appropriately use the in built camera. Here is the link.
I am relativly new on android but from what I have read is the every time the display rotates android creates a new instance of some sort. So you have to save the instance of the rotation and this is done with the following code:
/**
* Here we store the file url as it will be null after returning from camera
* app
*/
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save file url in bundle as it will be null on scren orientation
// changes
outState.putParcelable("file_uri", fileUri);
}
/*
* Here we restore the fileUri again
*/
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// get the file url
fileUri = savedInstanceState.getParcelable("file_uri");
}
If you click on the link you should go to bullet number 11. Avoiding NullPointerException after taking camera picture. The real hero here is Ravi Tamada who does an excellent tutorial on using the camera. I recommend reading the whole tutorial.
Again I am new at this so if there is any corrections on what I have wrote here please correct.
var camera = {
settings : {
quality : 50,
targetWidth : 1024,
targetHeight : 1024,
correctOrientation : true
}
};
var error = function(message) {
alert("Error happened while trying to get a picture", message);
};
document.addEventListener("deviceready", function() {
camera.toFile = function() {
this.settings.destinationType = navigator.camera.DestinationType.FILE_URI;
return this;
},
camera.toBase64 = function() {
this.settings.destinationType = navigator.camera.DestinationType.DATA_URL;
return this;
},
camera.fromCamera = function() {
this.settings.sourceType = navigator.camera.PictureSourceType.CAMERA;
return this;
};
camera.fromLibrary = function() {
this.settings.sourceType = navigator.camera.PictureSourceType.PHOTOLIBRARY;
return this;
};
camera.fromPhotoAlbum = function() {
this.settings.sourceType = navigator.camera.PictureSourceType.SAVEDPHOTOALBUM;
return this;
}
camera.get = function(callback) {
navigator.camera.getPicture(function(data) {
alert("taking a picture successful");
callback(data);
}, error, camera.settings);
};
}, false);
This is my small wrapper for the camera. And I call it like this:
camera.fromPhotoAlbum().toBase64().get(function(base64){});
About 20% of the time, the "alert("taking a picture successful");" is not called, while no error is shown. If I cancel taking a picture, an alert with the message "Error happened while trying to get a picture" is shown, so the error callback works.
Basically nothing happens. I've tested it on a Samsung Galaxy S2 on CM9 and a brand new HTC One X.
There was another question recently about this same problem that I answered. We ran into this at my company and solved it. It has more to do with the Android system than Phonegap.
What's happening is when you start the camera, your app goes into onStop(). While there, the Android system has the right to kill your app if it needs memory. It just so happens that memory usually gets low when the camera takes a picture and dumps it into memory, so there's a good chance your app will get killed while your user takes a picture.
Now that your app is dead, when the camera finishes, it restarts your app. That's why it's acting so weird; the camera comes back into your app, but not the same instance that it had before, so your callback never gets called, since it doesn't exist anymore.
You can reduce the frequency at which this occurs by reducing the quality of the picture and passing it by URI instead of data to your app, but the problem won't go away completely.
To work around the callback never happening, we made a Java callback that starts the camera and saves the picture to the same location every time it takes one. Then, when the app starts back up from getting killed, it looks in that location for the picture.
It's a weird solution to a stupid problem, but if your app gets killed, that camera callback simply won't happen. If you need more information on how to make the Java callback to do this, let me know and I'll put our code up here. Otherwise, take a look at this SO answer for more info.
EDIT: Here's the code we use in our main DroidGap activity:
private static final String folderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/appName";
private static final String filePath = "phonegapImage.jpg";
#Override
public void onCreate(Bundle savedState) {
//...The rest of onCreate, this makes the Java available in JavaScript
appView.addJavascriptInterface(this, "Camera");
}
public void takePhoto(final String callback) {
Log.v("Camera Plugin", "Starting takePhoto callback");
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(folderPath, filePath)));
startActivityForResult(intent, TAKE_PICTURE);
}
public String getPhotoUri() {
return Uri.fromFile(new File(folderPath, filePath)).toString();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
//Do whatever you need to do when the camera returns
//This is after the picture is already saved, we return to the page
}
break;
default:
Log.v("Camera", "Something strange happened...");
break;
}
}
Then, in your JavaScript, you can invoke the camera with:
Camera.takePhoto("onPhotoURISuccess");
//Then, to get the location of the photo after you take it and load the page again
var imgPath = Camera.getPhotoUri();
So, that's about it. Just make sure to change all of the path/file/page/etc names to what you want to use in your app. This will overwrite that image every time a picture is taken, but you can probably figure something out to dynamically name them if you don't want that. You can use that URI just as you would any other path in your JavaScript.
I'm working on an app that take picture by calling the Camera Intent. In the next Activity I use the URI of the image that I got and display the image. It works fine.
The problem is when I test in Sony Neo V device (ICS), the image get rotated 90 degrees (this is the screenshot). It doesn't happen when I test in HTC Desire device (Gingerbread) (this is the screenshot).
Here is my code:
Activity 1:
private final int CAMERA_REQUESTCODE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.memberform);
Button photo = (Button) findViewById(R.id.btn_photo);
photo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUESTCODE);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ( requestCode==CAMERA_REQUESTCODE ) {
if ( resultCode==RESULT_OK ) {
GlobalVar.member.setPhotoUri(data.getData());
} else if ( resultCode==RESULT_CANCELED ) {
} else {
Toast.makeText(this, "Unknown onActivityResult resultCode = " + resultCode, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Unknown onActivityResult requestCode = " + requestCode, Toast.LENGTH_SHORT).show();
}
}
Activity 2:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.memberdetail);
ImageView photo = (ImageView) findViewById(R.id.photo);
photo.setImageURI( GlobalVar.member.getPhotoUri() );
}
I've tried to detect if ( ImageView.getWidth()>ImageView.getHeight() ) then rotate90degrees(); but it doesn't work. And I'm hoping there's a general working code (works on any device) that solve this problem because it would be better than making a conditional if.
Any help & explanation would be appreciated. General working code would be greatly appreciated.
Many thanks
There seems to be a bug with other devices as well, not sure if all Samsung devices do it, but quite a few are doing it. I can confirm on my device Samsung infuse.
You may have to query the ContentResolver to get the orientation for each image and see if it needs rotating.