In my two to three application I have to used camera activity from application. User taken the picture using camera and set that image on image View.
It works for all devices excepting Droid X. When the user takes the picture from Droid X mobile, application is forced close.
Here is my code for start camera activity:
public void startCameraActivity()
{
_path = Environment.getExternalStorageDirectory() + "/default.jpg";
File file = new File( _path );
Uri outputFileUri = Uri.fromFile( file );
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
_taken = true;
bita = BitmapFactory.decodeFile( _path);
imv.setImageBitmap(bita);
}
}
So what should I do to run camera activity successfully in Droid X? I wasn't able to find what the problem is.
The issue here is not a memory issue as Andro has commented on this, and other similar postings.
The issue is simply related to how the Droid X camera intent differs from other devices's.
In my case, I was receiving a NullPointerException when attempting to grab my specified URI that I passed to the camera as using "intent.putExtra( MediaStore.EXTRA_OUTPUT, imageUri );". Some device camera's do not take in this extra properly, and those cases need to be handled separately. (see accepted answer here).
Also in my case, with a Droid X running 2.3.3, the returned intent was null as well. My debugger would detach during the camera intent, indicating that there was something wrong outside of my control. The best I could do is follow the accepted answer here.
So I added a capture for when my desired imageUri is null, and since my returned intent was null, I added logic to grab the most recent photo taken (using this as an example).
if ( imageUri == null ) {
String[] projection = new String[] { MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, MediaStore.Images.ImageColumns.DATE_TAKEN, MediaStore.Images.ImageColumns.MIME_TYPE };
final Cursor cursor = context.getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC" );
int column_index = cursor.getColumnIndexOrThrow( MediaStore.Images.Media.DATA );
cursor.moveToFirst();
fileUri = cursor.getString( column_index );
}
I hope this helps!
Related
I can successfully take a photo on an Android phone but not on a Nexus 10 tablet. The tablet returns a null value even though I took a picture. To initiate the photo take I use the following code:
String fileName = "myimage.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult( intent, RESULT_LOAD_IMAGE);
And my code on returning from taking the photo looks like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Log.d("selectedImage=", String.valueOf(selectedImage));
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
The activity returns a null value for selectedImage when using the Nexus tablet but returns a valid value for the Samsung Galaxy 4. What could be different between the 2 devices and how do I accommodate the differences? I should note that when taking a photo with the tablet, the photo is not placed in my gallery either. Any help is appreciated.
I would like to show an user experience like this:
to let user select images that he want... and then i'll use it.
How can i achieve this?
Thanks!
You can use intent to open default Gallery in Android and let user select image.
final static int RESULT_CHOOSE_IMAGE = 1;
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_CHOOSE_IMAGE);
After user selects image, onActivityResult() will be called which you will have to override as follows,
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == RESULT_CHOOSE_IMAGE && data != null)
{
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String bitmapPath = cursor.getString(columnIndex); // path to user selected image
cursor.close();
// get bitmap from bitmap path
Bitmap bitmap = BitmapFactory.decodeFile(bitmapPath , null);
}
}
Since bitmaps can be big in size, it may cause OutOfMemoryException while decoding from file. Refer this link for info regarding displaying bitmaps effeciently.
Android native gallery doesn't support multiple image selection by default, so you will have to use custom gallery for that purpose. See this link for tutorial .
In a future, try to be more specific in your questions so we can help you in a better way.
With the information you provided, i can just tell you that you could use a GridView to achieve what you want.
GridView Android Developers
I can take a video with an intent now what are the details to create an intent to start the default video trimmer activity? And check if it present on the device?
This solution relies on a version of the AOSP Gallery2 package being installed on the device. You can do it like this:
// The Intent action is not yet published as a constant in the Intent class
// This one is served by the com.android.gallery3d.app.TrimVideo activity
// which relies on having the Gallery2 app or a compatible derivative installed
Intent trimVideoIntent = new Intent("com.android.camera.action.TRIM");
// The key for the extra has been discovered from com.android.gallery3d.app.PhotoPage.KEY_MEDIA_ITEM_PATH
trimVideoIntent.putExtra("media-item-path", getFilePathFromVideoURI(this, videoUri));
trimVideoIntent.setData(videoUri);
// Check if the device can handle the Intent
List<ResolveInfo> list = getPackageManager().queryIntentActivities(trimVideoIntent, 0);
if (null != list && list.size() > 0) {
startActivity(trimVideoIntent); // Fires TrimVideo activity into being active
}
The method getFilePathFromVideURI is based on the answer of this question: Get filename and path from URI from mediastore
public String getFilePathFromVideoURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Video.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
videoUri is an Uri pointing to something like this: content://media/external/video/media/43. You can gather one by issuing an ACTION_PICK Intent:
Intent pickVideoUriIntent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickVideoUriIntent, PICK_VIDEO_REQUEST);
In onActivityResult get the uri like so:
....
case PICK_VIDEO_REQUEST:
Uri videoUri = data.getData();
...
This solution works on my Galaxy Nexus with Android 4.3 Jelly Bean.
I am not sure if this is available on all Android devices.
A more reliable solution may be to fork the Gallery2 app and put the TrimVideo activity together with its dependencies into a library that can be delivered with your app.
Hope this helps anyway.
Try this may it helps
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra("android.intent.extra.durationLimit", 30000);
intent.putExtra("EXTRA_VIDEO_QUALITY", 0);
startActivityForResult(intent, ActivityRequests.REQUEST_TAKE_VIDEO);
This code works well on API >=2.2, but the duration limit does not work on API 2.1
I have a piece of code that aims to launch the android camera, let the user take a photo and eventually keep the uri of the image once it is saved.
So far I have managed to get it working. However, it seems like a 50/50 chance that the app will crash with a NPE. I have no idea why this is happening
my code:
private Uri mImageCaptureUri;
private void doTakePhotoAction() {
String fileName = "temp.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mImageCaptureUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
startActivityForResult(intent, PICK_FROM_CAMERA);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
switch (requestCode) {
case PICK_FROM_CAMERA:
String[] projection = { MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(mImageCaptureUri, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String capturedImageFilePath = cursor.getString(column_index_data);
Intent intent = new Intent(this, CropImage.class);
intent.putExtra("image-path", capturedImageFilePath);
intent.putExtra("scale", true);
startActivity(intent);
break;
}
}
The NPE error refers me back to the line
Cursor cursor = managedQuery(mImageCaptureUri, projection, null, null, null);
and it is being thrown because mImageCaptureUri is null. Does anyone have any ideas on how to fix / work around this or why this is in fact happening
You really have to take to heart the first rule of Android Activities: Android may decide at any time to kill your activity, resetting all member variables in the process. It will simply rebuild the activity once it needs to handle onActivityResult... but as you initialized mImageCaptureUri in your specific handler, it will now be null.
The classic cause in your example would be an orientation change during the launched "capture an image" activity (orientation changes will typically kill the activity), but there is a myriad of other reasons why android would decide to terminate it (lack of memory, incoming calls, phone having a bad hair day, etc).
The solution is to store and restore state like mImageCaptureUri in onSaveInstanceState(), and restore it in the onCreate() of the Activity, i.e.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (savedInstanceState != null)
{
mImageCaptureUri = savedInstanceState.getParcelable("imageCaptureUri");
}
}
protected void onSaveInstanceState(Bundle outState)
{
outState.putParcelable("imageCaptureUri", mImageCaptureUri);
}
How can I get a user to select from the native gallery in Android, rather than other gallery-like applications such as ASTRO File Manager?
The following code gives a list of activities that can select an image:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
List<ResolveInfo> infos = activity.getPackageManager().queryIntentActivities(intent, 0);
If I then do something like this:
activity.startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CHOOSE_IMAGE);
then if the user has more than one application that can act like a gallery (such as ASTRO File Manager), s/he is prompted to select one of them, with no "set as default" option.
I don't want the user to be prompted to choose between them each time, so I'd like to just use the native gallery.
The hacky code sample below uses a whitelist to test for known native gallery names:
for (ResolveInfo info : infos) {
if ( 0==info.activityInfo.name.compareTo("com.cooliris.media.Gallery")
|| 0==info.activityInfo.name.compareTo("com.htc.album.CollectionsActivity")
) {
// found the native gallery
doSomethingWithNativeGallery();
}
}
Feels kinda dirty. Is there a better way? I suspect I'm missing something in my intent.
I didn't get last part of your code.
To start the native gallry what i did is -
public void upload(){
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/jpg");
photoPickerIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Pictures/image.jpg"));
startActivityForResult(photoPickerIntent, 1);
}
Call the upload() where you like to start the native gallery.
Then to get that image info i did -
/**
* Retrieves the returned image from the Intent, inserts it into the MediaStore, which
* automatically saves a thumbnail. Then assigns the thumbnail to the ImageView.
* #param requestCode is the sub-activity code
* #param resultCode specifies whether the activity was cancelled or not
* #param intent is the data packet passed back from the sub-activity
*/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_CANCELED) {
return;
}
else if(resultCode == RESULT_OK) {
Uri uri = intent.getData();
String path = getPath(uri);
Log.i("PATH", path);
data = path;
return;
}
uplad();
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
If you wouldn't get your answer, clear what you like to do