Intent is null in onActivityResult when taking a photo [duplicate] - android

I am using following code to get picture from camera. Except samsung it is working fine in other mobiles. please let me know what i am doing wrong.
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "temp" + File.separator);
root.mkdir();
final String fname = "img_" + System.currentTimeMillis() + ".jpg";
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = cordova.getActivity().getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam){
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
//FileSystem
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/jpeg");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Image Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
startActivityForResult(chooserIntent, PICK_FILE_REQUEST);
And onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FILE_REQUEST) {
if(data!=null){
Logv("Data","got some data");
}
}
}

Except samsung it is working fine in other mobiles
No, it is only working with the camera apps on the devices that you have tried. It generally will not work for ACTION_IMAGE_CAPTURE.
please let me know what i am doing wrong
You are assuming that ACTION_IMAGE_CAPTURE is supposed to return a result Intent. It is not required when you provide EXTRA_OUTPUT. You know where you asked for the image to be saved (the value you supplied to EXTRA_OUTPUT, so go look there for the image.
Note that there are some buggy camera apps that will ignore EXTRA_OUTPUT (storing the image where they want), so if the image from your ACTION_IMAGE_CAPTURE request is not in EXTRA_OUTPUT, you are out of luck.

Related

Android how can I pick image from camera or gallery at the same time with one Intent

Here's a implementation I found in a app named Pawoo. I can choose take photo or pick a image from system built-in gallery or third-party gallery at the same time.
I wonder how to achieve it with just one Intent. Because it seems not implements by third-party library.
I already know how to achieve it. Inspired by Intent to choose between the camera or the gallery in Android
The answer of qustion is not just one Intent. Simply, in my question screentshot, there's 3 actions, that's means 3 Intents. The key method is Intent.createChooser()
Here's my complete code:
public void click(View view) {
File file = getExternalFilesDir(Environment.DIRECTORY_DCIM);
Uri cameraOutputUri = Uri.fromFile(file);
Intent intent = getPickIntent(cameraOutputUri);
startActivityForResult(intent, -1);
}
private Intent getPickIntent(Uri cameraOutputUri) {
final List<Intent> intents = new ArrayList<Intent>();
if (true) {
intents.add(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI));
}
if (true) {
setCameraIntents(intents, cameraOutputUri);
}
if (intents.isEmpty()) return null;
Intent result = Intent.createChooser(intents.remove(0), null);
if (!intents.isEmpty()) {
result.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(new Parcelable[] {}));
}
return result;
}
private void setCameraIntents(List<Intent> cameraIntents, Uri output) {
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, output);
cameraIntents.add(intent);
}
}
Here's the my demo:
It's not one Intent. This dialog is a bottom sheet.

Allowing User to select Image from camera and Gallery dose not work for installed app like Camera360

I'm newbie in Android development. Well my application allows the user to select images from a gallery and to captures image taken from camera. Well it works perfectly fine while picking an image from a gallery and Native Camera but it dose not work when picking an image from an installed app like Camera360. Can anyone help me with this issue.
Below is my code to show a options to select images from Gallery and camera
File root = new File(Environment.getExternalStorageDirectory() + File.separator + "Test" + File.separator);
if (!root.exists())
root.mkdirs();
String fname = "img_" + System.currentTimeMillis() + ".jpg";
File sdImageMainDirectory = new File(root, fname);
Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
List<Intent> cameraIntents = new ArrayList<Intent>();
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
PackageManager packageManager = getPackageManager();
List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
String packageName = res.activityInfo.packageName;
Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
startActivityForResult(chooserIntent, PICK_IMAGE_REQUEST);
onActivityResult Method is implemeted below
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == PICK_IMAGE_REQUEST) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else
isCamera = action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
}
Bitmap yourSelectedImage;
if (isCamera) {
yourSelectedImage = BitmapFactory.decodeFile(outputFileUri.getPath());
} else {
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 filePath = cursor.getString(columnIndex);
cursor.close();
yourSelectedImage = BitmapFactory.decodeFile(filePath);
}
com.pkmmte.view.CircularImageView CircularImageView = (com.pkmmte.view.CircularImageView) findViewById(R.id.profileImage);
CircularImageView.setImageBitmap(yourSelectedImage);
}
}
}
The code works perfectly fine for both camera and gallery image picker. But it crashes for installed app like Camera360. Can some one help me regarding this
A Uri is not a file, and so you cannot pass it to decodeFile() on BitmapFactory. Your "pretend this Uri actually came from MediaStore" code will not work either.
Either use an image loading library like Picasso or Universal Image Loader, or have your own background thread that uses openInputStream() on a ContentResolver to read in the contents of that Uri, passing the stream to decodeStream() on BitmapFactory.
The code works perfectly fine for both camera and gallery image picker.
Only for the couple of cases that you tried. For example, if the "gallery image picker" returns an image that is on removable storage on Android 4.4+, even if you could get a filesystem path, you can't read it, as you don't have read access for arbitrary locations on removable storage.

Permission denied, but nothing fails

Every time a i startActivityForResult() , I get this line in logcat:
07-30 00:18:07.013: W/ActivityManager(2889): Permission denied: checkComponentPermission() owningUid=10095
But nothing actually fails, i don't get exceptions, everything works as it should.
I don't know what should i do about it, or if this can cause problems on other devices.
Here is the full code for the intent that i use:
// Determine Uri of camera image to save.
final File sdImageMainDirectory = getTempFile();
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent
.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[] {}));
startActivityForResult(chooserIntent, SELECT_PHOTO);`
Why is this happening and what can i do against it, how can i find out why do i get this message?

Android -- Camera Intent Returns Immediately

I have an activity that allows the user to select preview a photo that they select from the gallery or the camera. The problem I'm having is that the camera/gallery intent returns immediately, then shows the camera/gallery and returns nothing.
The basic flow of things is as follows: Fragment -> Application Subclass -> Top Activity -(startActivity)-> Photo Preview Activity -(in onCreate)-> Photo Chooser Intent
//In the application subclass
public static void launchImageSelector()
{
if(!(topActivity instanceof ImagePreviewActivity))
{
Intent i = new Intent(context, ImagePreviewActivity.class);
topActivity.startActivityForResult(i, kImageSelectorRequestCode);
}
}
///in ImagePreviewActivity class
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent chooser = createChooserIntent(createCameraIntent());
chooser.putExtra(Intent.EXTRA_INTENT, createOpenableIntent("image/*"));
startActivityForResult(chooser, 1);
}
//intent creaters(from android src)
private Intent createChooserIntent(Intent... intents)
{
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents);
chooser.putExtra(Intent.EXTRA_TITLE, "Choose Photo");
return chooser;
}
private Intent createOpenableIntent(String type)
{
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
// i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType(type);
return i;
}
private Intent createCameraIntent()
{
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File externalDataDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM);
File cameraDataDir = new File(externalDataDir.getAbsolutePath() +
File.separator + "browser-photos");
cameraDataDir.mkdirs();
String mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator +
System.currentTimeMillis() + ".jpg";
photoFileUri = Uri.fromFile(new File(mCameraFilePath));
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFileUri);
return cameraIntent;
}
What am I doing wrong here? What would cause the Chooser Intent to return immediately but also continue? Am I doing something fundamentally wrong here?
Thanks for the help!!
After hours of debugging the problem was in the Manifest file. In android, you can't start an activity for a result if the launch mode is set to singleInstance or singleTop
Found the answer here: Android - startActivityForResult immediately triggering onActivityResult

Android intent album gallery

I want to do a intent to the gallery for take a photo. It's easy to find a lot of examples that do this, but i want that open the gallery in my own album directly. it's possible?
You can try doing something like this:
private static final int SELECT_PICTURE = 1;
// ...
Intent in = new Intent();
in.setType("image/*");
in.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(in, pickTitle);
chooserIntent.putExtra
(
Intent.EXTRA_INITIAL_INTENTS,
new Intent[] { takePhotoIntent }
);
startActivityForResult(chooserIntent, SELECT_PICTURE);

Categories

Resources