I'm using the following code to record the camera in Android. How can I change the default codec (the default is H264) here?
private Uri fileUri;
//...
private void recordVideo() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
// set video quality
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
// start the video capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
}
// ...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE && resultCode == RESULT_OK) {
// play the video given the global fileUri
}
You cannot achieve this with ACTION_VIDEO_CAPTURE intent. You have to open the camera in your app and handle recording video by yourself. The official example using CameraX library are a good starting point.
To control the codec, you need an extra step. You can borrow from this answer, which enforces h264 – but you are free to choose MediaCodec.createEncoderByType() the way you like.
Related
I tried to get path from
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Complete action using"), 2);
But I did't get path from Marshamallow, I can get path from lollipop.
How I can get file path from internal storage?
I think you will have to use file provider for that.
Refer this.
https://developer.android.com/reference/android/support/v4/content/FileProvider
When you start an activity using startActivityForResult, the calling activity will get the results (if any) in onActivityResult. You should override that method in your calling activity...
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
}
This will method will get called as soon as the results are returned from the activity started by startActivityForResult. You can use the requestCode parameter to match (or filter out) the results you are expecting. In your case, you should check that requestCode is equal to 2 which is the value you provided in startActivityForResult.
If the requestCode is equal to 2, then you know that you need to process a PDF file. The intent parameter "could" have the path of that PDF file...
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if(requestCode == 2 && data != null){
//This is the uri to the file (which could be null)
Uri uri = data.getData();
if(uri != null){
Log.i("LOG_TAG", String.format("uri path = %s", uri));
}
}
}
Now, to browse and select files you will do it the following way (Android Kitkat and above...I think)...
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/pdf");
startActivityForResult(intent, SELECT_FILE_REQUEST_CODE);
Then you receive the selected PDF uri as mentioned above
I have the following code on my button, which allows me to click a button and choose a PDF file. How do I get the path of the PDF file, and actually do something with the file (because right now I can only click on the file and nothing happens). I want the image to be put in an API do to something to the image.
final Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("*/*");
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
You must override the onActivityResult in order to get what you need.
You will be able to get what you want (the file path) with the resulting intent. Like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if(resultCode == RESULT_OK) {
Uri fileUri = intent.getData();
}
}
I have a simple fragment that when user taps on the screen it will send an Intent to open the Camera app and then expect an image to be returned.
public void camera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap)extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
}
According to Google developer this should be all that is required.
After the picture has been taken and I press SAVE button in Camera app it returns back to my app.
I have set some breakpoints in onDestroy() and in onActivityResult(), it first destroys the fragment and creates a new one 2 times, then onActivityResult() is called and then it is .... destroyed again ... and created. So the image gets lost.
Why is this happening and how can I fix it?
Running on Samsung S4
I found a dirty fix to it (dirty imo looks pretty weird)
In the AndroidManifest.xml add this parameter to your Activity that handles the Camera Intent:
android:configChanges="orientation|screenSize"
The Activity gets destroyed because of the orientation change in the transition to and from the Camera app... with this parameter it wont destroy.
Did u try using an intent extra called EXTRA_OUTPUT which will store the photo in given uri
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent , Request_Id);
I am using onActivityResult to get an image taken from the camera intent. I would like to get the actual image and not the thumbnail. How can I do that. When I use data.getExtras().get("data") I get the thumbnail which is low quality. I do not wish to save the image locally as I will be uploading it to a server.
Camera Intent:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
onActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
}
}
How can I do that
Provide a Uri in EXTRA_OUTPUT in your Intent, pointing to a location where the third-party camera app can write the image.
I do not wish to save the image locally as I will be uploading it to a server.
Then either settle for the thumbnail or write your own camera code (rather than using a third-party app).
I am attempting to launch the built-in camera to take a picture, a picture that will have a name specified by the activity launching the camera. (code below)
When the camera returns, onActivityResult() goes straight to resultCode == Activity.RESULT_CANCELED. Any explanation for this and solutions would be greatly appreciated.
The camera indeed does take the image, I can see it in my sdcard with a file viewer, but its name is the stock one from the camera. How can I get the name of this taken image to be the one supplied by the activity?
Camera intent code
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File("Team image.jpg");
camera.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
camera.putExtra(MediaStore.Images.Media.TITLE, "Team image");
startActivityForResult(camera, PICTURE_RESULT);
activityresult code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == PICTURE_RESULT){
if(resultCode == Activity.RESULT_OK) {
if(data!=null){
Bitmap image = BitmapFactory.decodeFile(data.getExtras().get(MediaStore.Images.Media.TITLE).toString());
grid.add(image);
images.addItem(image);
}
if(data==null){
Toast.makeText(Team_Viewer.this, "no data.", Toast.LENGTH_SHORT).show();
}
}
else if(resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(Team_Viewer.this, "Picture could not be taken.", Toast.LENGTH_SHORT).show();
}
}
}
Have you mark the launch mode of your activity as "singleInstance"?
That may cause your first problem.
My camera goes normal when I remove the "singleInstance".
The two issues are likely related, having to do with the way you are creating the file reference that passes to the camera. If you want your image file to save to the SD Card, you need to create a file reference that includes a full-path to that location, not just a filename. For example, this code would save the image file on the SD card root:
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File(Environment.getExternalStorageDirectory(),"TeamImage.jpg");
camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
startActivityForResult(camera, PICTURE_RESULT);
I also changed your filename to not include a space; only because I'm not certain that the Camera application won't blow up on that piece also. Since the Camera is getting confused trying to open and write to your file location, that is likely why you always return with RESULT_CANCELED. You don't need the WRITE_EXTERNAL_STORAGE permission here, since the Camera app is doing the SD Card access.
One more note: I don't believe other MediaStore extras can be passed with this Intent. Typically, if you want metadata to be attached to your image, you have to insert the Uri reference with that metadata into the MediaStore ContentProvider prior to saving the image to disk.
Hope that helps!
Not sure what's wrong with your code, here's what works for me:
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
and
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case CAMERA_PIC_REQUEST:
Bitmap b = (Bitmap) data.getExtras().get("data");
if (b != null) {
updateThumbnail(b);
if (mBitmap != b) {
b.recycle();
}
}
break;
}
}