how to get the images from device in android java application - android

In my application I want to upload the image.
For that I have to get images from gallery in android device.
How do I write code that accomplishes this?

Raise an Intent with Action as ACTION_GET_CONTENT and set the type to "image/*". This will start the photo picker Activity. When the user selects an image, you can use the onActivityResult callback to get the results.
Something like:
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri chosenImageUri = data.getData();
Bitmap mBitmap = null;
mBitmap = Media.getBitmap(this.getContentResolver(), chosenImageUri);
}
}

Related

How Can I Get the Uri of Image if I Open the Gallery and Select Image and share with my App

How Can I Get the Uri of Image if I Open the Gallery and Select Image and share with my App, but Image is not showing in ImageView
Open gallery for getting Image Uri
private static final int REQUEST_SELECT_IMAGE = 100;
private void handleOpenGallery() {
Intent intentSelect = new Intent(Intent.ACTION_GET_CONTENT);
intentSelect.setType("image/*");
startActivityForResult(Intent.createChooser(intentSelect, getString(R.string.select_picture)),
REQUEST_SELECT_IMAGE);
}
Uri of selected Image
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SELECT_IMAGE) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
shareImage(uri);
}
}
}
Share Image
private void shareImage(Uri imageUri) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, "Share to"));
}
You can follow below code by Raghu to open gallery,
if you only want to open gallery then you can use,
Intent intentSelect = new Intent(Intent.ACTION_PICK);
Add check for read/write permissions if OS version is above Marshmallow(6.0)
If you still don't get the uri then in onActivityResult when you will fetch the uri try the getPath method from here
Add check if the Android OS version is above Nougat(7.0) or not, as above nougat while fetching file uri you may get "FileUriExposedException" refer this link for more info

image not cropping after cropped

I am attempting to crop an image using intent after the image is selected from the gallery. Here is my snippet of code
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
//******code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
Here I am calling the above snippet with PICK_IMAGE_REQUEST intent handle
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
try {
Uri filePath = data.getData();
what could be wrong since I am using the same intent after cropping which is PICK_IMAGE_REQUEST
There are no documented and supported "crop" extras for ACTION_GET_CONTENT or any other standard Android Intent. Nor is there a documented and supported standard Intent action for cropping. There is no requirement for any device to have apps that support undocumented and unsupported extras, actions, etc.
If you want to crop an image, use any one of a number of existing open source libraries for image cropping.

How to get name of captured image in android onActivityResult

how to get image name from captured image/intent data. any one suggest me. i need through intent data only
Here is my code:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, PIC_FROM_CAMERA);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK && data != null){
Bitmap photo = (Bitmap) data.getExtras().get("data");
}
}
AS from the Android documentation here you could not get any name for your image since android wont name for your file but you could get the time when the image was taken and the dimension of the image from This answer You could name the image by yourself and send that bitmap.

Error during taking picture

I'm writing an application which have to take a picture and send it to a web service.
There is the code I use to :
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
outputFileUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"/appicture.jpg"));
Log.i("URI", outputFileUri.toString());
i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(i, cameraData);
The URI looks like :
file:///sdcard/appicture.jpg
But if I put "outputFileUri" var in the i.putExtra, my app just quit.
If not, I can take the picture but I can't get her URI then unable to send it to my webservice.
EDIT 1 :
Error log ( on the activity resylt)
06-26 09:17:46.108: I/Cam error(699): java.lang.NullPointerException
EDIT 2 :
If I remove the "outputFileUri", I correctly got the Image. But, then I'm unable to convert the Bitmap into File to be able to send it.
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
_bmp = (Bitmap) extras.get("data");
}
EDIT 3 :
The problem was from
_bmp = (Bitmap) extras.get("data");
And the picture is correctly save in the sd card.
Add WRITE_EXTERNAL_STORAGE permission in Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
EDIT:
if you are passing Image Uri in Intent for Capturing image then get image as in onActiityResult as:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == cameraData) {
File picture = new File(Environment.getExternalStorageDirectory() + "/appicture.jpg");
Uri imguri=Uri.fromFile(picture);
}
super.onActivityResult(requestCode, resultCode, data);
}
and if you want to get image as data in onActiityResult then Launch Camra as:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, cameraData);
in onActivityResult:
onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == cameraData) {
Bundle extras = data.getExtras();
_bmp = (Bitmap) extras.get("data"); //Get data here
}
}
}
If you already added the permissions and the path is correct. Could you please try removing ".jpg" at the end of your path
Please add this in Manifest File
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Happy Coding..

take picture from android camera and send it to web server

im build some apps that call camera activity..
im just to take picture from my apps and send it to web server..
but i can't get my image path..
im always getting NullException Error when try to get image path..
here's my code when calling camera activity :
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
this.startActivityForResult(camera, PICTURE_RESULT);
and this is code for activity result :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICTURE_RESULT){
if (resultCode == Activity.RESULT_OK) {
takePicture(data);
} else if (resultCode == Activity.RESULT_CANCELED) {
}
}
}
protected void takePicture(Intent data) {
Bundle b = data.getExtras();
pic = (Bitmap) b.get("data");
if (pic != null) {
imagePicture.setImageBitmap(pic);
}
}
is there something wrong with my code?
Thanks
Ok, I see your problem. You're not setting the path to begin with. Please look at this doc.
http://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE
So you see, when you call ACTION_IMAGE_CAPTURE you are not passing it the extra EXTRA_OUTPUT that tells the application where the picture is going to be stored. This EXTRA_OUTPUT is the path to the file.
So right under where you make the intent do this:
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
URI pictureUri = Uri.fromFile(new File(<path to your file>));
camera.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

Categories

Resources