if (resultCode == Activity.RESULT_OK && requestCode == 1
&& null != data)
{
Uri selectedImage = data.getData();
InputStream imageStream =getActivity().getContentResolver().openInputStream(selectedImage);
System.out.println("dfsdf");
Bitmap bitmap2 = BitmapFactory.decodeStream(imageStream);
basically onactivityresult thats how i read, and i get selected image as null. when i selected my image from file manager ( /sdcard)..however when i selected from uhf player..i select from camera or screenshot, it works fine
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"), 1);
//Here is some sample code to pick photo from gallery or get from camera.
//Declare the following
private static final int SELECT_PHOTO = 100;
private static final int CAMERA_REQUEST=101;
//way to call startactivityforresult select photo from gallery(sd card)
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
//way to call startactivityforresult select photo from camera
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
//onActivityResult method
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if(resultCode == RESULT_OK){
//pick image from gallery(sd card)
if(requestCode==SELECT_PHOTO)
{
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
imageView_Babypic.setImageBitmap(yourSelectedImage);
}
//pick image from camera
else if (requestCode==CAMERA_REQUEST) {
Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
imageView_Babypic.setImageBitmap(photo);
}
}
}
//at last use this for camera use in your Manifest file
<uses-permission android:name="android.permission.CAMERA"/>
Use this code to launch you intent chooser -
Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent takePhotoIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
String pickTitle = this
.getResources()
.getString(R.string.edit_general_select_or_take_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);
and your onActivityResult will be like this -
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (imageReturnedIntent != null) {
if (imageReturnedIntent.getData() != null) {
Uri selectedImage = imageReturnedIntent.getData();
}
}
}
Hope this helps.
Related
I want to open an image stored in local storage in the gallery from my app. But it always shows that the image is unsupported.
Here is the code:
File fileStr = new File (Environment.getExternalStorageDirectory() + "/Storifier/test.jpg");
Uri uri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", fileStr);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/jpg");
startActivity(intent);
The Uri that I get for the file is
content://com.purple.myapplication.provider/external_files/Storifier/test.jpg
The image is stored in Storifier folder in the storage
I am a newbie!
try this code
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);
add this in your activity result
#Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
image_view.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(PostImage.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(PostImage.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
}
}
and add this in manifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
i have 4 diffrent imageview and try to click on all to upload diffrent images like LIcence,Rc,Profile etc . I want to uploaded image from gallery or camera dialog..
like this layout.
like uper layout example
Please check my updated answer. This is just an example. Hope you understand from this
ImageView profile_pic;
private static final int SELECT_PICTURE1 = 100;
private static final int SELECT_PICTURE2 = 101;
private static final int SELECT_PICTURE3 = 102;
private static final int SELECT_PICTURE4 = 103;
picture1 = (ImageView) view.findViewById(R.id.picture1);
picture2 = (ImageView) view.findViewById(R.id.picture2);
picture3 = (ImageView) view.findViewById(R.id.picture3);
picture4 = (ImageView) view.findViewById(R.id.picture4);
picture1.setOnClickListener(this);
picture2.setOnClickListener(this);
picture3.setOnClickListener(this);
picture4.setOnClickListener(this);
#Override
public void onClick(View v) {
if (v.getId() == R.id.picture1) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE1);
}
if (v.getId() == R.id.picture2) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE2);
}
if (v.getId() == R.id.picture3) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE3);
}
if (v.getId() == R.id.picture4) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE4);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE1) {
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
String path = selectedImageUri.getPath();
Log.e("image path", path + "");
pricture1.setImageURI(selectedImageUri);
}
}
if (requestCode == SELECT_PICTURE2) {
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
String path = selectedImageUri.getPath();
Log.e("image path", path + "");
picture2.setImageURI(selectedImageUri);
}
}
if (requestCode == SELECT_PICTURE3) {
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
String path = selectedImageUri.getPath();
Log.e("image path", path + "");
picture3.setImageURI(selectedImageUri);
}
}
if (requestCode == SELECT_PICTURE4) {
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
String path = selectedImageUri.getPath();
Log.e("image path", path + "");
picture4.setImageURI(selectedImageUri);
}
}
}
}
private void imageBrowse() {
if(camera){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}else{
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, PICK_IMAGE_REQUEST);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if(requestCode == PICK_IMAGE_REQUEST){
Uri picUri = data.getData();
filePath = getPath(picUri);
uri =picUri;
Log.d("picUri", picUri.toString());
Log.d("filePath", filePath);
imageView.setImageURI(picUri);
}
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
EDIT
Since you are using recyclerview or listview kind of view, you can associate tag to each viewholder and then use that tag for resolving imageview. Now you can get unique tag based on position in view. For eg in recyclerview you can use getAdapterPosition().
Associate OnClicks of each imageview with a request code. In onActivityResult resolve them and put images accordingly.
I would like to suggest to refer this site, It's implementation is easy and straight forward...
http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/
In addition to the perfectly working answer by #paras, if someone wants to include the Camera option also then please refer to the below steps:
Add a dialog box which pops-up on image click and asks to chose either Camera or Gallery:
final CharSequence[] items = {"Camera", "Gallery", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(PhotoActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Camera")) {
openCamera();
} else if (items[item].equals("Gallery")) {
openGallery();
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
Use this method to open the Camera intent:
private void openCamera() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, REQUEST_CAMERA);
}
Now in the onActivityResult() method, include this to check whether the intent is from Camera:
if (requestCode == REQUEST_CAMERA) {
Bundle bundle = data.getExtras();
final Bitmap bitmap = (Bitmap) Objects.requireNonNull(bundle).get("data");
imageView.setImageBitmap(bitmap);
}
I'm trying to open an image using intent.
The path of the image in my storage is /storage/emulated/0/nitp/download/logo.png.
My code is
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DEFAULT);
intent.setDataandType(Uri.parse("/storage/emulated/0/nitp/download/logo.png"),"image/*");
startActivity(intent);
I also tried putting file://storage/emulated/0/nitp/download/logo.png
and content://storage/emulated/0/nitp/download/logo.png
What is the path I should use?
Solved
Have to use file:///storage/emulated/0/nitp/downloads/logo.png
For Pick image from media Storage or SD Card:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
And For get the path of Image and set in ImageView:
#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) {
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
imgpath= MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
imageViewProfileImage.setImageBitmap(imgpath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Hope this work.
I am asking the user for the access to the gallery through the code as a listener here:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
However, I am confused as to how I would set a variable to the photo selected.
Where would I put the code to set a variable as the photo selected?
Thanks :)
First you have to override onActivityResult to get the uri of the file selected image
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == SELECT_PHOTO) {
if (resultCode == RESULT_OK) {
if (intent != null) {
// Get the URI of the selected file
final Uri uri = intent.getData();
useImage(uri);
}
}
super.onActivityResult(requestCode, resultCode, intent);
}
}
Then define useImage(Uri) to use the image
void useImage(Uri uri)
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
//use the bitmap as you like
imageView.setImageBitmap(bitmap);
}
You can do it like this.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Here we need to check if the activity that was triggers was the Image Gallery.
// If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
// If the resultCode is RESULT_OK and there is some data we know that an image was picked.
if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
// Let's read picked image data - its URI
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
// Do something with the bitmap
// At the end remember to close the cursor or you will end with the RuntimeException!
cursor.close();
}
}
Alternative for Akash Kurian Jose answer
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
I always use
fun getBitmap(file: Uri, cr: ContentResolver): Bitmap?{
var bitmap: Bitmap ?= null
try {
val inputStream = cr.openInputStream(file)
bitmap = BitmapFactory.decodeStream(inputStream)
// close stream
try {
inputStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
}catch (e: FileNotFoundException){}
return bitmap
}
It works both for photos from gallery and photos from camera.
Larger issue about it: Picasso unable to display image from Gallery
Open Gallery using this method:
private void openGallery(){
if (Build.VERSION.SDK_INT <19){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.select_picture)),GALLERY_INTENT_CALLED);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, GALLERY_KITKAT_INTENT_CALLED);
}
}
Then you are able to read convert Uri to Bitmap using afromentioned ContentResolver.openInputStream or set image ImageView.setImageUri(Uri)
If you want to display the selected image to any particular ImageView.
Suppose we have RC_PHOTO_PICKER = 1 then these lines of code should do the magic
private void openPhotoPicker() {
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, false);
startActivityForResult(Intent.createChooser(photoPickerIntent,"Complete Action Using"), RC_PHOTO_PICKER);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK && data != null) {
Uri pickedImage = data.getData();
//set the selected image to ImageView
mImageView.setImageURI(pickedImage);
}
}
And simply call the openPhotoPicker() method afterwards
I’m trying to get an image using the built in gallery. It works fine in the emulator and It opens only gallery but on real device it give me multiple chooses one of them is file manager which enable me to choose any type of files even apk files of course the app crash after that
I have this code
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch(requestCode){
case SELECT_PICTURE:
Uri selectedImageUri = data.getData();
break;
}
}
}
Try to use
....
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PICTURE);
....
public void ChoosePicture(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
{
if (resultCode == RESULT_OK)
{
Uri photoUri = data.getData();
if (photoUri != null)
{
try {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
bMap_image = BitmapFactory.decodeFile(filePath);
ImageView img = (ImageView) findViewById(R.id.gallery1);
img.setImageBitmap(bMap_image);
}catch(Exception e)
{}
}
}// resultCode
}// case 1
}// switch, request code
}// public void onActivityResult
mmh, somehow it changed the position of my last few "}".
This code will let you select an image from the gallery and then show it on an imageview.
I use this code on my device, and works like a charm.
Try using this for your intent:
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
If you are wanting to always use the stock Gallery Application I don' think you need to use an Intent Chooser so you might be able to change your startActivity to this:
startActivityForResult(intent, SELECT_PICTURE);