Open an Image Using Uri in Android's Default Gallery Image Viewer

Open an image using URI in Android's default gallery image viewer

Ask myself, answer myself also:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** replace with your own uri */

It will also ask what program to use to view the file.

Unable to open captured photo in Android's default image viewer using photo's URI


android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=/storage/emulated/0/Android/data/com.packagename.inventoryapp/files/Pictures/InventoryApp/1526635391354.jpg }

That is not a valid Uri.

A Uri has a scheme. Yours does not. Yours resembles a bare filesystem path. In principle, you could convert that to a Uri using Uri.fromFile().
However, on Android 7.0+, using such a Uri will fail with a FileUriExposedException.

Instead, use the File with FileProvider.getUriForFile(), and provide that Uri to your ACTION_VIEW Intent. Be sure to also call addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) on that Intent, to allow third-party apps to read the content identified by that Uri.

How to open image in default image viewer of Android

Finally I got this working.

1. Start File Manager to select file (In my case it is image file)

int PICKFILE_REQUEST_CODE=33; // class property

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT); // It helps to get Image Uri
intent.setType("image/*"); // Filter only images to open
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(intent, PICKFILE_REQUEST_CODE);

2. Use onActivityResult event to get result of selection of the intent with PICKFILE_REQUEST_CODE

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == PICKFILE_REQUEST_CODE ) {
if(resultCode == Activity.RESULT_OK){
Uri imageUri = data.getData();
Intent intent = new Intent(this, ImageViewer.class);
intent.putExtra("imageUri", imageUri.toString());
startActivity(intent); // Start new intent to see the full sized image ImageViewer
}
}

3. And withing ImageViewer we can do all staff with Image

public class ImageViewer extends AppCompatActivity {

private ImageView imgView;
private String fullScreenInd;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_viewer);

Uri myUri = Uri.parse(getIntent().getExtras().getString("imageUri"));
imgView = (ImageView)findViewById(R.id.fullImageView);
imgView.setImageURI(myUri);

imgView.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
imgView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
imgView.setAdjustViewBounds(false);
imgView.setScaleType(ImageView.ScaleType.FIT_XY);

}
}

Open default Image Viewer

In listview on itemclick u just add this .

            File file = new File(itemList.get(position));
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse("file://" + file.getAbsolutePath());
intent.setDataAndType(data, "image/*");
startActivity(intent);

in itemlist is your arraylist.

android show image in default gallery viewer

Gallery application does not accept URL for an image as part of the Intent. You need to save the image first. Then you can launch the default image viewer with something like this:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + "/sdcard/test.jpg"), "image/*");
startActivity(intent);

Open Image with intent from internal storage

Try with this :

    Intent intent = new Intent();  
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://" + file.getAbsolutePath());
intent.setDataAndType(uri,"image/*");
startActivity(intent);

Thanks.

Using the default android image Viewer to show pics

try this you can use android.support.v4.view.ViewPager

Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows.

<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</android.support.v4.view.ViewPager>

demo code

ViewPager viewPager;
ArrayList<String> imageArray;
imageArray = new ArrayList<>();
viewPager = findViewById(R.id.cspl_viewPager);
imageArray.add(R.drawable.bg);
imageArray.add(R.drawable.bg);
imageArray.add(R.drawable.bg);
imageArray.add(R.drawable.bg);
imageArray.add(R.drawable.bg);
imageArray.add(R.drawable.bg);
ImageAdapter adapter = new ImageAdapter(this, imageArray);
viewPager.setAdapter(adapter);

now create ImageAdapter like this

    public class ImageAdapter extends PagerAdapter {

Context context;
ArrayList<String> imageArray;

public ImageAdapter(Context context, ArrayList<String> imageArray) {
this.context = context;
this.imageArray = imageArray;
}

@Override
public int getCount() {
return imageArray.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, 50);
imageView.setLayoutParams(layoutParams);

int padding = context.getResources().getDimensionPixelSize(R.dimen.font_size_10);
imageView.setPadding(padding, padding, padding, padding);

// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

Glide.with(ProfileActivity.this)
.load(R.drawable.bg)
.into(imageView);


((ViewPager) container).addView(imageView, 0);

return imageView;

}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}


Related Topics



Leave a reply



Submit