如何在图库中打开图片?

3

有一条像这样的图片路径:

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

我有一个ImageView,它加载了图片的缩略图。我使用Picaso库的一侧:

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

创建“当单击ImageView时触发的事件”:

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

        startActivity(intent);
    }

如何在画廊中以全屏大小打开图像?在哪里找到实现它的方法,但是在drawable资源中没有,我需要通过远程路径访问图片。


请查看此帖子:http://theopentutorials.com/tutorials/android/imageview/android-how-to-load-image-from-url-in-imageview/ - bGorle
我有一个ImageView,其中加载了图片的小副本。我使用Picaso库的一侧:Picasso.with(getApplicationContext()).load(path).into(imageview);我在imageView中获取图像URL。当单击图像时,我需要在画廊中打开整个屏幕。 - duddeniska
试图获取Picasso存储文件的位置,向相册应用发送“Intent”。 - bGorle
我写了以下代码:public void click_img(View v){ Intent intent1 = new Intent(); intent1.setAction(Intent.ACTION_VIEW); Uri imgUri = Uri.parse(path_img); intent1.setDataAndType(imgUri, "image/*"); startActivity(intent1); }但是我遇到了一个错误:没有找到活动。 - duddeniska
1个回答

1
最直接的方法是将图像保存到SD卡,然后使用Intent打开默认的相册应用程序。
既然已经在使用Picasso,那么可以通过以下方式完成:
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 是 Picasso 提供的一个类。只需覆盖 onBitmapLoaded 方法,以便将您的图像保存到 SD 卡中。我为您提供了一个名为 saveImage 的示例方法。有关更多信息,请参见 this answer

您还需要在 Manifest 中添加此权限:

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

如何在画廊中打开一张图片,使其以画廊的全屏尺寸显示?我没有看到图片上的onclick事件。 - duddeniska
是的,你仍然需要为你的ImageView实现onClickListener。 - adao7000
这时候你就需要调用你的Intent(就像你之前展示的那样,使用你保存图片到SD卡的路径)。 - adao7000
1
这段代码可以在不将图片保存到SD卡的情况下正常工作。public void click_img(View v){ Intent intent1 = new Intent(); intent1.setAction(Intent.ACTION_VIEW); Uri imgUri = Uri.parse(path_img); intent1.setDataAndType(imgUri, "image/*"); startActivity(intent1); } - duddeniska

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接