如何在图库中打开图像?

时间:2015-07-27 16:26:15

标签: android

图像的路径如下:

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

我有ImageView,它载有图片的小副本。我使用Picaso库的一面:

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

在Imageview上创建事件Onclick():

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

        startActivity(intent);
    }

如何在图库中打开图片,全屏大小?在哪里可以找到实现它的方法,但是在可绘制的资源中,我需要它通过远程路径到图片?

1 个答案:

答案 0 :(得分:1)

最简单的方法是将图像保存为SD,然后使用Intent打开默认的图库应用程序。

由于您已经在使用毕加索,以下是使用该库的方法:

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是毕加索提供的课程。只需覆盖onBitmapLoaded方法,即可将图像保存到SD。我为您提供了saveImage的示例方法。有关详细信息,请参阅this answer

您还需要将此权限添加到您的Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
相关问题