Take picture and name it afterwards - java

I want to take a picture with the standart system service
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
and AFTERWARDS I want to give it a custom name and directory or path (which should happen in another activity after I've taken the photo with the camera activity)
The problem is that if I create a file with all the attributes (name, path) and give it to the intent I cant do it in the activity after having taken the photo instead I would need to determine its attributes before I open the Intent(take the photo).
(as suggested in the Google article:https://developer.android.com/training/camera/photobasics#TaskPath)
Should I just get the fullsize Bitmap then open the other activity and then save it and determine its attributes?
Is there a way to do it as suggested in the article but somehow the other way around?(create the File afterwards)
Let me know if you have any idea.
I apreceate any help.
Thank you!

I want to take a picture with the standart system service
Your code is launching any one of hundreds of possible camera apps. The specific app might have been pre-installed by a device manufacturer, or it might be an app that the user installed.
The problem is that if I create a file with all the attributes (name, path) and give it to the intent I cant do it in the activity after having taken the photo instead I would need to determine its attributes before I open the Intent(take the photo).
Correct.
Should I just get the fullsize Bitmap then open the other activity and then save it and determine its attributes?
There is no means of having ACTION_IMAGE_CAPTURE give you a "fullsize Bitmap" directly. You can either get a thumbnail-sized Bitmap or have it write a full-sized photo to a location of your choice.
Is there a way to do it as suggested in the article but somehow the other way around?(create the File afterwards)
No. However, there is nothing stopping you from opening the photo via its file in your activity, then modifying that photo and writing it back out. You could overwrite the original file, or you could write to some new location.
So, for example, if your concern is that you do not want the photo to be in a user-accessible location until your activity is done with it, you could pass a Uri to ACTION_IMAGE_CAPTURE that points to a private location in getCacheDir(), then have your activity write the final version of the photo to a file on external storage.

Related

Set image changes when I move from one fragment to another, How to keep it forever even when I restart the app?

please help I could not add code, it is throwing error , I'm new.
ActivityResultLauncher<Intent> picActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
// your operation....
Uri pic = data.getData();
profile.setImageURI(pic);
}
}
}
It depends on the device's android version. If it's 10 or lower than that, then you can simply save the file path in the Sharedpreference so you can access it later and load the image from there (You can do it in android 10 with requestLegacyExternalStorage of course or you can go with the 2nd option which I provided below).
But, if you're writing the code for android 11 or higher, then there are only three standard ways to do it.
1. Using SAF (Storage Access Framework) :-
You can get the storage access permission of that perticular folder everytime you pick an image from there. But this is not the best option when your app is doing it multiple times. (what if it's photo editor app or social media or something like that?!)
2. Manage all files permission :-
You can go with the All files access permission but it's also too much for the small task and you also have to give clarification to google play when your app has that permission. So it's also the very last option.
3. Accessing from internal app directories - THE BEST WAY! :-
You can go with this option with almost every app!
All you have to do is just take read storage permission, access the file using file descriptor, write it to the internal app directory (it can be either external files directory or cache directory), then you'll have a lifetime access of the image. You can save the path to Sharedpreference and access it anytime.
If you want to save edited image to the gallery then it will also be easy because you already have both read and write permission to that image saved in internal app directory.
That's it. I know the answer is lengthy but it's worth it. :)

How to open a file in external storage from an application? (Android)

I've added a button to my application which is supposed to open the download folder of the phone, and from there you should be able to click on files that were stored there, from the same app. Right now im saving some data there.
Problem is; I cant open the saved files in the folder.
I can see the files stored right there, but when I press one of them you immediatley go back to the app and not the file that you pressed.
Is there something I'm missing? Are you not supposed to open files stored in external storage from another app?
I've tried adding permissions in manifest and checkSelfpermission for checks in runtime, but with no success.
Here's the button for opening download folder:
private void openSavedLocation(){
if (ContextCompat.checkSelfPermission(ExportAndImport.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(ExportAndImport.this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
intent.setDataAndType(uri, "text/xml");
startActivity(Intent.createChooser(intent, "Open Folder"));}
I can open the file perfectly when Im opening it outside the app, not via this "createChooser". What could i be missing?
Any help is appreciated.
but when I press one of them you immediatley go back to the app and not the file that you pressed
That is what your code does. ACTION_GET_CONTENT says "let the user pick a piece of content". It does not say "open that piece of content in some other app". There is no single Intent action for saying "let the user pick a piece of content, then open that piece of content in some other app".
Is there something I'm missing?
If you want to try to open the XML in some other app:
Use startActivityForResult(), not startActivity(), for your ACTION_GET_CONTENT request (and get rid of the createChooser() bit)
Override onActivityResult() to get the result of the user's choice
If the user chose something (i.e., you get RESULT_OK in onActivityResult()), create an ACTION_VIEW Intent wrapped around the Uri that you get from the Intent passed into onActivityResult(), and call startActivity() on the ACTION_VIEW Intent
If, instead, your objective is to open this XML in your app, you would:
Use startActivityForResult(), not startActivity(), for your ACTION_GET_CONTENT request (and get rid of the createChooser() bit)
Override onActivityResult() to get the result of the user's choice
If the user chose something (i.e., you get RESULT_OK in onActivityResult()), get the Uri of the content from the Intent passed into onActivityResult(), then use ContentResolver to do something useful with that Uri (e.g., openInputStream() to read in the content)
Here's the button for opening download folder
ACTION_GET_CONTENT uses the MIME type. It will not necessarily honor your supplied starting Uri.

How to start file browser app from Android application?

I have an App that is started from "share via" menu and get the list of the selected files as an input. Now, what I would like to do is to let the user be able to run file browsing app from my App and then get back the results.
I know for example that I can start phonebook and obtain the choosen contact(s) with following code:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
So the question is: is there a similar way to run the file browser and to get in return a list of all files selected?
EDIT: the "possible duplicated post" is actually only partially similar, as it ask how to start the file manager inside a specific path, and by the way hasn't an accepted answer. What I really need, if it is possible, is to start the file manager (if there is one) to a specific path and then get in return the selected files.
thank you all very much
Cristiano

Button to start the Gallery on Android

I'm trying to make a button in my App open the built in gallery.
public void onClick(View v) {
Intent intentBrowseFiles = new Intent(Intent.ACTION_VIEW);
intentBrowseFiles.setType("image/*");
intentBrowseFiles.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentBrowseFiles);
}
This results in an error message "The application Camera (process com.android.gallery) has stopped unexpectedly."
If I set the Intent action to ACTION_GET_CONTENT it manages to open the gallery but then simply returns the image to my app when a picture is selected which is not what I want.
I'm trying to make a button in my App open the built in browser.
Your question subject says "Gallery". Your first sentence in the question says "browser". These are not the same thing.
If I set the Intent action to ACTION_GET_CONTENT it manages to open the gallery but then simply returns the image to my app when a picture is selected which is not what I want.
Of course, actually telling us "what [you] want" would just be too useful, so you are making us guess.
I am going to go out on a limb and guess that you are trying to open the Gallery application just as a normal application. Note that there is no Gallery application in the Android OS. There may or may not be a Gallery application on any given device, and it may or may not be one from the Android open source project.
However, for devices that have the Android Market on them, they should support an ACTION_VIEW Intent with a MIME type obtained from android.provider.MediaStore.Images.Media.CONTENT_TYPE.

EXTRA_OUTPUT ignored on Eris, causes data to return null on G1

So I'm trying to launch the Camera activity using the following code:
//In public void captureImage()
...
Intent cameraIntent = new Intent(MediaStore.ACTION_CAPTURE_IMAGE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File("/sdcard/image.jpg")));
startActivityForResult(cameraIntent, REQUEST_CAMERA);
And then to handle the result:
//In onActivityResult()
...
case REQUEST_CAMERA:
Intent intent = new Intent (CurrentScreen.this, NextScreen.this);
intent.putExtra(data);
startActivity(intent);
CurrentScreen.this.finish();
...
Where I use intent.putExtra(data) to attach the small bitmap to the intent, and use it as a thumbnail in the next activity, and the full sized file is supposedly saved as /sdcard/image.jpg.
This is the expected behavior (according to the documentation), to have a small bitmap for a thumbnail, and a large file saved. However when testing this on a G1 and an Eris, I have been seeing some strange behavior.
On the G1:
Although the resultCode shows RESULT_OK, the intent data that is returned to the result handler is null.
Also EXTRA_OUTPUT seems to be completely ignored, I have no idea where it is saving the image.
On the Eris:
The intent data comes back OK
EXTRA_OUTPUT is also ignored, but it is saving the images to the regular media store at /sdcard/dcim/100media
So my question is this: is there any way to get consistent behavior for what I am trying to do using the standard camera activity? I could write up a custom activity to try and get it to work the way I want, but I'd prefer to avoid that route.
I do not have answers to your question as I am new to the Java/Android development world. But I am attempting something similar to what you are doing, except I want to simply take the picture then attach it to an Email message.
I implemented part of your example and was able to verify that the camera created the file I specified and that if I use the same file name for the next picture that it overwrites the previous file which is what I would expect.
But what I was really going to say is perhaps you will have to test if the pat "/sdcard/..." actually exists or not. Also you could possibly simplify your process by passing the path to the next activity.
Good Luck,
Jamie Irwin

Categories