Android-what to do with chosen image? - java

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

Related

Change camera recording codec programmatically in Android

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.

From Uri to Environment.DIRECTORY_DCIM

A user chooses a directory through the following code:
public void startChoose(View view) {
Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
i.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(Intent.createChooser(i, "Choose directory"), 8010);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 8010:
mypath = data.getData().toString();
break;
}
}
His directory is stored in mypath as following (e.g. with DCIM):
content://com.android.externalstorage.documents/tree/primary%3ADCIM
Now, is it possible to go from content://com.android.externalstorage.documents/tree/primary%3ADCIM to Environment.DIRECTORY_DCIMand how?
Asked differently, I would like to get the variable Environment.DIRECTORY_DCIM for mypath when the User chooses the DCIM directory. How can one do that?
Now, is it possible to go from content://com.android.externalstorage.documents/tree/primary%3ADCIM to Environment.DIRECTORY_DCIMand how?
No. The Uri that you get back does not have to correspond to a file (e.g., it points to a document in Google Drive). Even if it corresponds to a file, it does not have to reside in a directory that is tied to Environment (e.g., it points to a file in a custom directory).
Please just use the Uri.

How to get file path for pdf in android Marshmallow

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

How to browse folder in android and get the path of folder selected

When I click a button,show a file browser,I can choose a folder and return it's path.I get this path to copy file to that path.
But I have no idea of How I could implement this.
I have yet looking for this question in Stackoverflow but I haven't find a clear answer to my question.
I saw some libary of filebrowserview like "https://github.com/psaravan/FileBrowserView",but not work.
Use intent for that!
First start start activity for result like this:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
Override this method in your activity, it will get called when activity you just started returns. You can handle result codes such as canceled or successful.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String Fpath = data.getDataString();
//TODO handle your request here
super.onActivityResult(requestCode, resultCode, data);
}
Another approach is to use library such as NoNonsense-FilePicker.

Camera Intent result woes

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

Categories

Resources