Image unsupported when using intent to view image in gallery - android

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"/>

Related

ImageView Load ImageUri

I have ImageView with image choosen by user from gallery:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
and:
#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);
ImageView img = findViewById(R.id.img);
img.setImageBitmap(selectedImage);
//
ImageURI = Uri.parse(String.valueOf(imageUri));
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(getApplicationContext(), "You haven't picked Image",Toast.LENGTH_LONG).show();
}
}
Becouse, I need to save Uri of this image to use it later I've created String ImageURI which value is assigned to String Value of imageUri (as you can see above)
But when I try to load this image to imageview;
ImageView.setImageUri(ImageURI);
image doesn't load. I don;t get any error.
Sample Uri which I was using: content://com.miui.gallery.open/raw/%2storage%2Femulated%2F0%2FDCIM%2Camera%2FIMG_20190521_005650.jpg

Path of Image to be passed in Uri.parse to open an image using intent

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.

intent returns null on picture

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.

How can i catch picture that was taken?

I new in the android developing.
I want to develop simple application that will be able to take a picture using the cell phone camara and show it on the screen of the cell phone.
Is there some simple example that i can use ? or some code that can help me learn how to do it ?
Thanks for any help
to start camera you use
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 0);
and here you have the handeling
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
Try this.. Use the below code in onCreate
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
URI mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"pic_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, CAMERA_RESULT);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
Then OnActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
//Here you will get path of image stored in sdcard then pass it to next activity as your desires..
mImagePath = extras.getString("image-path");
mSaveUri = getImageUri(mImagePath);
Bitmap mBitmap = getBitmap(mImagePath);
// here mBitmap is assigned to any imageview and you can use it in for display
}
}
private Uri getImageUri(String path) {
return Uri.fromFile(new File(path));
}
private Bitmap getBitmap(String path) {
Uri uri = getImageUri(path);
InputStream in = null;
try {
in = mContentResolver.openInputStream(uri);
return BitmapFactory.decodeStream(in).copy(Config.ARGB_8888, true);
} catch (FileNotFoundException e) {
//Log.e(TAG, "file " + path + " not found");
}
return null;
}
}

How to get bitmap providing a URI, java.io.FileNotFoundException: No content provider: /sdcard/Hello/1310610722879.jpg

I've read the example to do this:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri imageUri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
}
}
But i get java.io.FileNotFoundException: No content provider: /sdcard/Hello/1310610722879.jpg
My code is here:
Uri uri1 = Uri.parse(Config.getPhotoPath(this));
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri1);
attachButton.setImageBitmap(bitmap);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Any ideas how to make it work?
Ok I messed around, u have to do this:
Uri uri1 = Uri.parse("file://" + Config.getPhotoPath(this));
Ok I messed around, u have to do this:
Uri uri1 = Uri.parse("file://" + Config.getPhotoPath(this));
Or you can do
File file = new file(Config.getPhotoPath(this));
Uri uri1 = Uri.fromFile(file);

Categories

Resources