I'm trying to implement a function to pick a photo from android built-in gallery (not from SD card) or taking a photo using camera. But "No Activity found to handle Intent act=android.intent.action.PICK" error was returned. My code is as follows:
private void ShowPickDialog() {
new AlertDialog.Builder(this)
.setTitle("Add Photo")
.setNegativeButton("Select from Photos", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.INTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(intent, 1);
}
})
.setPositiveButton("Take a Pic", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
.fromFile(new File(Environment.DIRECTORY_PICTURES,
"image.jpg")));
startActivityForResult(intent, 2);
}
}).show();
}
Can someone plz tell me what the problem is? Also which directory should be used at this line "intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
.fromFile(new File(Environment.DIRECTORY_PICTURES,
"image.jpg")));
Thanks in advance!
Related
Here is my code. I'm having problems with showing and saving the pic picked from the user's gallery, whilst when I do it from the camera it works just fine for me.
here is how the user is reffered to his gallery-
if (v.getId() == R.id.btnAddDucuments) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setMessage("to add documents");
alertDialog.setPositiveButton("from camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// user choose to take a picture
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAM_REQUEST);
}
});
alertDialog.setNegativeButton("from gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// user choose to pick a photo from gallery
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), GET_FROM_GALLERY); }
});
alertDialog.show();
and here is how the picture is supposed to be saved. I need it both as a String (so I can use it further on) and in the ImageView that is in this avtivity (named imgTheDocument).
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==GET_FROM_GALLERY && resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
StringImgDocument = BitMapToString(bitmap);
imgTheDocument.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When I run this code the activity stops after pressing on the wanted picture. I can't understand where is the problem. I have used this code before and it worked just fine.
I'll appriciate any help!! Thanks:)
my manifest permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
the intent to pick image(This is Error free and works on all android versions!)
Intent intent;
if (Build.VERSION.SDK_INT < 19){
intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, GET_FROM_GALLERY);
} else {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, GET_FROM_GALLERY);
}
This is for onActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==GET_FROM_GALLERY && resultCode == Activity.RESULT_OK) {
//you can both use the selectedImageUri or yourSelectedImagePath for working the Image you picked,send it somewhere or store it in database or etc..
Uri selectedImageUri = data.getData();
String yourSelectedImagePath = selectedImage.tostring;
//I just use the Uri to display the selected image in an imageview
yourImageView.setImageUri(selectedImage);
}
For android studio, I am doing a module where I have to take an image as well as all type files and store into our server. (i.e my sql I am using common backend.) Through Spring MVC, I am doing this project. I am not able to save the file location to mysql.
We have to save only the uploaded location, but that location is our server.
I tried to take the image from mobile, but after taking the image it is not saving. I also granted permission to externalsd.
my code is:
//image selection
private void selectImage() {
Dialog dialog = new Dialog(getActivity());
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose Image Source");
builder.setItems(new CharSequence[] { "Gallery", "Camera" },
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
switch (which) {
case 0:
Intent intent = new Intent(
Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
Intent chooser = Intent
.createChooser(
intent,
"Choose a Picture");
startActivityForResult(
chooser,
ACTION_REQUEST_GALLERY);
break;
case 1:
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(
cameraIntent,
ACTION_REQUEST_CAMERA);
break;
default:
break;
}
}
});
builder.show();
dialog.dismiss();
}
I have the following requirement:
From the mobile application , I want to open the default gallery of the Android tablet / device , which will contain both the image and video files and select them.
Is there any option to view both image and video files on Android Gallery?
Kindly provide your suggestions.
I think, There is no such an option to open both images videos at a time..... But we can filter it before open like this
final CharSequence[] options = {"Images", "Videos", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(OpenGallery.this);
builder.setTitle("Select From...");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Images")) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, 1);
} else if (options[item].equals("Videos")) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, 1);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
dialog.dismiss();
}
});
builder.show();
just try this..
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"content://media/internal/images/media"));
startActivity(intent);
Hope it helps ..!
try this
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("images/*,video/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
I'm trying to implement a function to pick a photo from android built-in gallery (not from SD card) or taking a photo using camera. After the image is picked it will be cropped by the user. An ImageView will also be updated with the cropped image. Now after picking an image from gallery, I cannot crop it -- "No Activity found to handle Intent act=com.android.camera.action.CROP" error was returned. And I also can't seem to update the ImageView with the selected image. Can someone please tell me what was wrong? Thanks.
My code is as follows:
private void ShowPickDialog() {
new AlertDialog.Builder(this)
.setTitle("Add Photo")
.setNegativeButton("Select from Photos", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),3);
}
})
.setPositiveButton("Take a Pic", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
.fromFile(new File(Environment.DIRECTORY_PICTURES,
"image.jpg")));
startActivityForResult(intent, 2);
}
}).show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
startPhotoZoom(data.getData());
break;
case 2:
File temp = new File(Environment.getExternalStorageDirectory()
+ "/test.jpg");
startPhotoZoom(Uri.fromFile(temp));
break;
case 3:
if(data != null){
setPicToView(data);
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(intent, 3);
}
private void setPicToView(Intent picdata) {
Bundle extras = picdata.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
//update ImageView with selected image
mPic.setImageBitmap(photo);
}
}
Android Does Not Have a Crop Intent
you can use libraries instead
Android CropImage
Hi I found the following code snippet from another question of stackoverflow to open Camera and Gallery Intent chooser combined. Here's the code.
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT,null);
galleryIntent.setType("image/*");
galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("Code", OPEN_CAMERA); <-- Not working
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INTENT, galleryIntent);
chooser.putExtra(Intent.EXTRA_TITLE, "Snap Option");
chooser.putExtra("Code", OPEN_GALLERY); <-- Not working
Intent[] intentArray = {cameraIntent};
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooser,1);
What I want to do is check which intent chooser was clicked when I will be in the onActivityResult . But as mentioned in the code I tried to putExtra params to differentiate and in the onActivityResult I am doing the following to get the code.
int Code = intent.getExtras().getInt("Code");
But this gives me a NullPointerException . How can I do this please help ?
You have to used something for pick the image from..
Like alert box,dialog,custom dialog and many more.i am just guide you how to used alertdialog.
final CharSequence[] items = { "Take Photo", "Choose from Library","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
//you can take photo from camera
/*Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, REQUEST_CAMERA);*/
} else if (items[item].equals("Choose from Library")) {
//you can pick photo from library/gallery
/* Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);*/
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();