I want to start camera activity in my android app and I know how to do that. I want to ask when the camera activity finishes, how can I check as if it was the picture or the video taken by the user?
UPDATED
I have a dialog where it asks 2 things.
New Photo or Video
Existing Photo or Video
If it's no. 1, it means camera will be started and user can either take a picture or the video and it will return to the activity.
If it's no.2, it mean gallery will be started having pictures and videos for a user to select one and will return back to the activity.
Hello Umair,
I have done this type of application I searched many time but I didn't get any proper solution so I changed your my menu & they are now
1)Take New Photo
2)Take New Video
3)Existing Image/Video
Process will be like this
1)I use an global variable
2)So when user click on menu one I sets global variable value to 1
3) Start the activity for result like below
try{
System.gc();
String fileName = System.currentTimeMillis()+".jpg";
String mPathImage = Environment.getExternalStorageDirectory()+ "/" + fileName;
File file = new File(mPathImage);
Uri outputFileUri = Uri.fromFile( file );
Intent mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mIntent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
startActivityForResult(mIntent, 1);
mValue=1;
}catch(Exception e){
}
If User click on menu 2 I change value of global variable to 2
& starts the activity for result like below.
try {
System.gc();
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, 1);
mValue=2;
}catch(Exception e){}
If user click on 3rd menu I set value to 3
& start the activity for result like below.
try{
System.gc();
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent,1);
mValue=3;
}catch(Exception e){}
}
This will show all the images & video's in mobile
Then finally when activity gets closed use global variable to check whether user want to new image or video or existing image/video.
Hope this will help you..
Related
I have been trying to integrate the Adobe Creative SDK in my app. I have added one extra activity which launches camera and gallery and saves the image in an ImageView.
I successfully passed the image to another activity but for using the Adobe Creative SDK you need an uri path.
After providing the uri path I am getting an error -
'AdobeImageIntent()' has private access in 'com.adobe.creativesdk.aviary.AdobeImageIntent'
here is the onCreate method -
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);
Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imageBitmap");
String path = bmp.toString();
Uri imageUri = Uri.parse(path);
Intent imageEditorIntent = new AdobeImageIntent().Builder(this)
.setData(imageUri)
.withOutput(Uri.parse("file://" + getFilesDir() + "my-pic-name.jpg")) // output file destination
.withOutputFormaat(Bitmap.CompressFormat.JPEG)
.withOutputSize(MegaPixels.Mp5)
.withOutputQuality(90)
.build();
startActivityForResult(imageEditorIntent,1);
Intent cdsIntent = AdobeImageIntent.createCdsInitIntent(getBaseContext(),"CDS");
startService(cdsIntent);
imageView = (ImageView) findViewById(R.id.imageView);
}
why I am getting this error?
Thanks for help!
Hope I would get answers!!
Replace
Intent imageEditorIntent = new AdobeImageIntent().Builder(this)
^^^^^
with:
Intent imageEditorIntent = new AdobeImageIntent.Builder(this)
^^^^^
Possible issues with the code above
Try removing the cdsIntent and see if your code runs.
Without more information, it's hard to know, but it could be that both your cdsIntent and your imageView assignments are coming too late in onCreate().
Taking a photo from your app
You can create a new camera Intent and start it like this:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(cameraIntent, 400); // Can be any int
}
The results from the camera will come back to you in onActivityResult().
Handling the results from the camera Intent
How to handle the results will depend on what your app does. Google has a nice overview on the subject on the Android Developer Portal.
If you have further questions beyond the scope of this one, it may be good to make a new question to help get this one more focused.
I have a problem, it is i using camera of android with call MediaStore.ACTION_IMAGE_CAPTURE
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
filepath = getOutputMediaFile();
uri = Uri.fromFile(filepath);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(cameraIntent, 0);
When i capture and then it don't auto save, i must be click to buttom save or buttom not, while i just need to save.
Button save and button not
This not comfortable, so how to set auto save and pretermit this step?
That's a function of the camera app. You would have to wrote your own code for capturing an image with the camera.
I have a weird problem!
My application calls the phone's camera to take a picture.
The photo is taken and stored in the ad hoc directory successfully
However, when I open the photo is not displayed (black screen)
and when I send the photo by email I can open it in my pc.
Here is the code :
String imageName = txtNomPhoto.getText().toString()+JPEG_FILE_SUFFIX;
File dossier = new File(Environment.getExternalStorageDirectory(),"/DossierPhotos");
if (!dossier.exists()){
if (dossier.mkdir()){
}
}
File mFichier = new File(dossier.getAbsolutePath(),imageName);
Uri fileUri = Uri.fromFile(mFichier);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, PHOTO_RESULT);
Thank you for your quick help.
I am trying to record a video in Android using the MediaStore.ACTION_VIDEO_CAPTURE intent. I can record the video in the default library album, but I cannot store anywhere else. I have tried to use the intent parameter EXTRA_MEDIA_ALBUM:
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_MEDIA_ALBUM, "My app videos");
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, 0);
I have also tried to insert the video row in the MediaStore before the video itself.
ContentValues videoValues = new ContentValues();
videoValues.put(MediaStore.Video.Media.TITLE, "My app video at" + System.currentTimeMillis());
videoValues.put(MediaStore.Video.Media.ALBUM, "My app videos");
Uri videoUri= getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoValues);
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, 0);
And I have also tried to modify the metadata once the video is inserted, to see if it is in a different album once I open the gallery application.
protected void onActivityResult(final int requestCode, final int resultCode, final Intent dataIntent) {
...
Uri contentUri = dataIntent.getData();
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.ALBUM, "My app videos");
int result = getContentResolver().update(contentUri, values, null, null);
'result' is 1, so the row value is actually changed, but it is not in "My app videos" album once I open it.
I have also tried different solutions explained in the Android documentation but with any Uri an IllegalArgumentException (Unknown URL file). I miss a handy method like MediaStore.Images.Media.insertImage
How have you dealt with this problem?
I have a similar problem, although my code is on two parts, one that deals Photos and the other that deals Videos, the same process and scenario gives two different results, the following thread explains more!
http://www.androidquestions.org/threads/618-Intent-doesn-t-keep-video-extras-after-capturing-the-video!?p=1761#post1761
I would be thankful if this issue has any solutions!
I'm developing an app for Android 2.1 upwards. I want to enable my users to select a profile picture within my app (I'm not using the contacts framework).
The ideal solution would be to fire an intent that enables the user to select an image from the gallery, but if an appropriate image is not available then use the camera to take a picture (or vice-versa i.e. allow user to take picture but if they know they already have a suitable image already, let them drop into the gallery and pick said image).
Currently I can do one or the other but not both.
If I go directly into camera mode using MediaStore.ACTION_IMAGE_CAPTURE then there is no option to drop into the gallery.
If I go directly to the gallery using Intent.ACTION_PICK then I can pick an image but if I click the camera button (in top right hand corner of gallery) then a new camera intent is fired. So, any picture that is taken is not returned directly to my application. (Sure you can press the back button to drop back into the gallery and select image from there but this is an extra unnecessary step and is not at all intuitive).
So is there a way to combine both or am I going to have to offer a menu to do one or the other from within my application? Seems like it would be a common use case...surely I'm missing something?
You can try doing something like this:
// ...
// Within your enclosing Class
// ...
private static final int SELECT_PICTURE = 1;
// ...
Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String pickTitle = "Select or take a new Picture"; // Or get from strings.xml
Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
chooserIntent.putExtra
(
Intent.EXTRA_INITIAL_INTENTS,
new Intent[] { takePhotoIntent }
);
startActivityForResult(chooserIntent, SELECT_PICTURE);
To see how to handle the activitiy's result, please refer to this question
Note: a critical point is how to determine whether the camera or gallery was used. That is shown in this code example: https://stackoverflow.com/a/12347567/294884
UPDATE: The other answer, using EXTRA_INITIAL_INTENTS, is a better one at this point. At the time I wrote my answer, EXTRA_INITIAL_INTENTS did not yet exist, as it was added in API Level 5.
So is there a way to combine both or
am I going to have to offer a menu to
do one or the other from within my
application?
Write your own gallery that has the features you desire.
I would think a menu would be simpler.
Seems like it would be a common use
case...surely I'm missing something?
The developer next to you will think the gallery should allow you to pick from the local gallery or else hop out to Flickr to make a selection from there. Another developer will think the camera should not only allow to "take a picture" via the camera but to "take a picture" via choosing something from the gallery, inverting things from the way you envision it. Yet another developer will think that the gallery should allow picking from the local gallery, or Flickr, or the camera, or a network-attached webcam. Still another developer will think that the gallery is stupid and users should just pick files via a file explorer. And so on.
All of this in an environment (mobile phones) where flash for the OS is at a premium.
Hence, IMHO, it is not completely shocking that the core Android team elected to provide building blocks for you to assemble as you see fit, rather than trying to accommodate every possible pattern.
You can proceed in this way in your Activity:
private static final int REQUEST_CODE_PICTURE= 1;
/**
* Click on View to change photo. Sets into View of your layout, android:onClick="clickOnPhoto"
* #param view View
*/
public void clickOnPhoto(View view) {
Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String pickTitle = "Take or select a photo";
Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { takePhotoIntent });
startActivityForResult(chooserIntent, REQUEST_CODE_PICTURE);
}
Then, add always in your Activity the method onActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_PICTURE && resultCode == Activity.RESULT_OK) {
if (data == null) {
return;
}
try {
InputStream inputStream = getContentResolver().openInputStream(data.getData());
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imgPhoto.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
My Answer is almost identical to #Macarse solution but I also add an additional intent to show gallery apps (Ex: Google Photos) and is written in Kotlin:
val REQUEST_CODE_GET_IMAGE = 101
private fun addProfileImage() {
val pickImageFileIntent = Intent()
pickImageFileIntent.type = "image/*"
pickImageFileIntent.action = Intent.ACTION_GET_CONTENT
val pickGalleryImageIntent = Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
val captureCameraImageIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val pickTitle = "Capture from camera or Select from gallery the Profile photo"
val chooserIntent = Intent.createChooser(pickImageFileIntent, pickTitle)
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(captureCameraImageIntent, pickGalleryImageIntent))
startActivityForResult(chooserIntent, REQUEST_CODE_GET_IMAGE)
}
Extract image from result intent:
private var imageTempFile: File? = null
private var imageMimeType: String? = null
private fun extractImage(intent: Intent?) {
val imageUri = intent?.data
imageUri?.let {
Glide.with(this)
.load(imageUri)
.into(profileImageCiv)
imageTempFile = MediaUtils.copyContentFromUriToCacheFile(this, imageUri, Settings.DIRECTORY_CACHE_TEMP_PROFILE_IMAGE)
imageMimeType = MediaUtils.getMimeType(this, imageUri)
} ?: run {
intent?.extras?.get("data")?.let { bitmap -> // Bitmap was returned as raw bitmap
Glide.with(this)
.load(bitmap)
.into(profileImageCiv)
imageTempFile = MediaUtils.writeBitmapToCacheFile(this, bitmap as Bitmap, Settings.DIRECTORY_CACHE_TEMP_PROFILE_IMAGE)
imageMimeType = "image/jpeg" // The bitmap was compressed as JPEG format. The bitmap itself doesn't have any format associated to it
} ?: run {
imageTempFile = null
imageMimeType = null
Log.e("Intent data is null.")
Log.d("Error during photo selection")
}
}
}