How do I open an image in the gallery?

There is an image path like this:

String path = "http://mysyte/images/artist/artist1.jpg"

      

I have an ImageView that loads a small copy of an image. I am using the Picaso library side:

Picasso.with(getApplicationContext()).load(path).into(imageview);

      

Create Onclick () event in image viewport:

public void click_img(View v){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);

        startActivity(intent);
    }

      

How to open image in gallery, full gallery size? Where can I find a way to implement it, but is there in drawable resources and I need to get this via a remote image path?

+3


source to share


1 answer


The easiest way to do this is to save the image to SD and then use intent to open the default gallery app.

Since you are already using Picasso, here's how you can do it with this lib:

private Target mTarget = new Target() {
      @Override
      public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
          // Perform simple file operation to store this bitmap to your sd card
          saveImage(bitmap);
      }

      @Override
      public void onBitmapFailed(Drawable errorDrawable) {
         // Handle image load error
      }
}

private void saveImage(Bitmap finalBitmap) {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");    
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-"+ n +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
           FileOutputStream out = new FileOutputStream(file);
           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

    } catch (Exception e) {
           e.printStackTrace();
    }
}

      



Target

is a class provided by Picasso. Just override the onBitmapLoaded method to save the image to SD. I have provided you with a sample saveImage method. See this answer for more information.

You also need to add this permission to your manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

      

+1


source







All Articles