简体   繁体   中英

How to open an image in the gallery?

There is a path to the image like this:

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

I have ImageView, which is loaded a small copy of the picture. I use the side of the library Picaso:

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

Create Event Onclick () on Imageview:

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

        startActivity(intent);
    }

How to open an image in the gallery, full screen size of the gallery? Where to find a way to implement it, but there in drawable-resources, and I need that to it is through the remote path to the picture?

The most straightforward way of doing this is to save the image to SD and then use an Intent to open the default gallery app.

Since you're already using Picasso, here's a way to do it using that 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 so that it saves your image to SD. I provided a sample method for saveImage for you. See this answer for more info.

You'll also need to add this permission to your Manifest:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM