As the title suggests, i'm having an option to upload an image in my app.
I would like to have two options: Click a new picture & Select from gallery.
Gallery selection is working fine on all devices using this code:
Intent in = new Intent();
in.setType("image/*");
in.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(in, getString(R.string.selectpicture)), 100);
The problem is with Click a new picture.
I want to use other camera apps installed on the device to get the image.
This code should save the image user clicks at the specified path.
Intent m_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageUri = getImageUri();
m_intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(m_intent, REQUEST_IMAGE_CAPTURE);
But what happens is, the EXTRA_OUTPUT is not respected by all camera apps.
Also if memory is low, my app is killed by the system which makes things more complicated.
So, what's the best way to enable user to click a new picture and get the image path in my app?
If using third party libraries is better, which one's are reliable?
the EXTRA_OUTPUT is not respected by all camera apps.
Any time you delegate anything to a third party app, you can run into problems.
Also if memory is low, my app is killed by the system which makes things more complicated.
You need to handle this anyway. Save the value you put in EXTRA_OUTPUT in the saved instance state Bundle.
what's the best way to enable user to click a new picture and get the image path in my app?
If you wish to stick with ACTION_IMAGE_CAPTURE, I'd go with this triage:
If there is an image in the location you provided to EXTRA_OUTPUT, use it
If not, and getData() on the Intent given to you in onActivityResult() returns a Uri (and not null), use that (and if you truly need a File, use ContentResolver and openInputStream() with the Uri and copy the contents into your own file)
If neither are true, and getParcelableExtra("data") returns a value, save that Bitmap to a file
If none of those are true, suggest to the user to get a different camera app, perhaps pointing them to one that you have tried and know works
Or, take the picture yourself, directly or using a library like mine.
Related
what is the correct way how I should form the intent to show content from my app in 3rd party viewers? I need to show images in gallery (or any other image viewer), pdfs in some pdf reader,..
Data gets server through a content provider which implements the openFile() method and returns a output pipe..
ParcelFileDescriptor[] pipe=ParcelFileDescriptor.createPipe();
...
ParcelFileDescriptor.AutoCloseOutputStream stream = new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1]);
PipeThread pipeThread = new PipeThread(fileContents, stream);
pipeThread.start();
return pipe[0];
For images I use this:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, uri);
I'm creating then a chooser for this intent as usual that's not the issue..
My problem is that although I see for example the Photos app in the chooser, I just cannot open the file in it..It just only opens the gallery with my images.
It's working when I use the action send, apps like gmail, drive, dropbox,..all of them are able to correctly read the image from the provider.
Also Skitch seems to be the only one app which I have tested it on that is able to open the image also using the Intent.ACTION_VIEW action..
Please don't tell me I should just send the URI, I really need to provide the file as a stream, or somehow as a serie of bytes (IPC limitations would be probably against this). I can't save the file to a public directory.
So the issue was that have been setting Intent type and data in two separate method calls..
What I didn't know is that Intent.setType() clears its data and Intent.setData() clears its type..
When I set both data and type through the Intent.setDataAndType() method call, it works even for URI pointing to a stream.
Unfortunately the final implementation is still not working flawlessly everywhere.
It works in default android gallery app, in G+ Photos app, in QuickPic, in Sony gallery app, but it does not work in default HTC gallery neither in default Samsung gallery.
Its just a pity, that its actually not that much dependent on my implementation as on how is it implemented in the 3rd party viewer app.
I am currently trying to create an app which when taking a picture using the camera, when saving the image, it saves to a specific folder location and if the folder doesn't currently exist on the phone, it creates the folder and saves the file to that location. My code does not currently work, although I have tried. Could you please look at my code and advise me on what I need to do.
Here is the code:
else if(v==camera){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File newDirectory = new File(Environment.getExternalStorageDirectory() + "App_Pictures/");
String filename = new String("image1.jpeg");
File outputFile = new File(newDirectory, filename);
outputFile.mkdirs();
startActivity(intent);
}
It looks like you're trying to get an external app to take a picture for you. If this is the case, you need to use startActivityForResult, not startActivity, because you want to receive the resulting photo. Then you will receive the result in the onActivityResult method of your activity. This process is described in detail here.
If you actually want your own app to take the picture, instead of an external app, you'll need to use a completely different approach. Here is an app that starts recording a video right when it is launched, and saves it to a directory on the SD card, so perhaps it's a useful starting point if you want to do it this way.
I'm writing an app that uses the camera. I want to lauch an intent to allow users to annotate the resulting image with lines and text, AND I would like to provide the user with a list of appropriate image editting apps they can use, but I'm coming across these problems:
1. Not all image editting apps appear in the list when I perform this code:
editIntent = new Intent();
editIntent.setAction(Intent.ACTION_EDIT);
Uri imageToEditUri = selectedPhotoLocation; // Uri of existing photo
String imageToEditMimeType = "image/*";
editIntent.setDataAndType(imageToEditUri, imageToEditMimeType);
startActivityForResult(Intent.createChooser(editIntent,"Edit Image"), IMPLICIT_EDIT_IMAGE);
Is there a way to get a list of apps that will respond to Intent.ACTION_EDIT?
2. PS Express is the only app I've found that returns the Uri of the editted image in the data.getDate() Uri returned to OnActivityResult(), with other apps the user is forced to save, remember the location, and reselect the editted image.
Is there a way to know what apps return the Uri of the image to OnActivityResult()
Not all image editting apps appear in the list when I perform this code
Just because an app implements image editing does not necessarily mean that it is set up to allow third parties to link to their image-editing activities.
Is there a way to get a list of apps that will respond to Intent.ACTION_EDIT?
If you mean that you want to do this programmatically at runtime, see Jedil's answer.
Is there a way to know what apps return the Uri of the image to OnActivityResult()
No, except for those applications that have documentation on how developers should integrate with them.
First question answer:
try queryIntentActivities
Intent editIntent = new Intent(android.content.Intent.ACTION_EDIT);
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(editIntent, 0);
I searched the Internet for quite a while without finding a solution.
I want to let the user pick images of his gallery using an intent.
For this i use a GET_CONTENT intent:
Intent intentBrowseFiles = new Intent(Intent.ACTION_GET_CONTENT);
intentBrowseFiles.setType("image/*");
intentBrowseFiles.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intentBrowseFiles,1);
But this only works for one image. Is there any possibility to make it work for more than one, or do I have to rebuild my own gallery for this?
The documentation clearly says:
Output: The URI of the item that was picked. This must be a content: URI so that any receiver can access it. (emphasis mine)
So, no, there doesn't seem to be a way to use the default Intents to select multiple items. Having an "add more" button should be easier than developing your own gallery, though.
I execute the following code:
Uri uri =
Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
imageIdStr); Intent intent = new
Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setData(uri);
activity.startActivity(intent);
which opens the image viewer activity but I don't want to see the navigation arrows as well as the items in the menu. I only want the zoom controls. Is it possible to have it like that ?
That depends on the application that's actually displaying the image. On Android 2.0 and older, it's probably the Gallery app. On many Android 2.1+ apps, it's probably CoolIris' gallery. If the user has one of the many gallery apps from the market installed, it's up to those apps. I highly doubt there's a standardized extra to control that.
If you're really concerned about what the image display looks like, then you should handle that yourself. If you're really just trying to display an image (and maybe add pinch-zoom support?), it's a reasonable scope.