2014-02-28 27 views
7

我正在使用毕加索帮助缓存图像。我如何访问毕加索的缓存图像来制作共享意图?

问题是,我如何访问下载的图像来制作共享意图?

有什么想法?谢谢!

+0

您可以截取该视图的位图对象,然后保存在目录中并使用Uri.parse()分享它; – Piyush

+0

有没有一种方法来实现它?我不想将它保存到另一个目录中。 – longkai

+0

意味着你已经将它保存在目录中了吗? – Piyush

回答

3

我希望你能理解我的问题:-)

对不起,我的延迟,我发现了一个解决方案,但不是一个好...

首先,我真的搜查了一会儿,看了毕加索的代码。看起来你应该提供你自己的下载器和其他东西。但是,为什么我应该使用lib ...

然后,我想这是毕加索的设计/架构,只是缓存文件在内部存储。也许是因为外部存储并不总是可用的(比如用户可能会将他的SD卡插入到他的计算机中),或者可能是因为外部存储没有内部那么快......这是我的猜测。总之,其他应用程序无法访问当前应用程序的内部存储,因此共享无法完成。

因此,我做了一个非常普通的解决方案。我只是等待毕加索给Bitmap,并将其压缩到外部文件中的文件,然后进行分享。这似乎是一个不好的解决方案,但它真的解决了这个问题,是的...

你应该知道外部缓存目录是否可用。如果没有,你不能分享。你需要把压缩任务放在后台线程中,等待缓存的外部文件......这看起来像是一个不好的解决方案吗?我想是这样...

下面是我的项目的代码,你可以试试...

private boolean mSaved; // a flag, whether the image is saved in external storage 
private MenuItem mShare; 
private Intent mIntent; 
private ShareActionProvider mShareActionProvider; 
private File mImage; // the external image file would be saved... 


private Target target = new Target() { 
    @Override 
    public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) { 
     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       FileOutputStream os = null; 
       try { 
        String dir = CatnutUtils.mkdir(getActivity(), Constants.FANTASY_DIR); // check the exteral dir avaiable or not... 
        String[] paths = Uri.parse(mUrl).getPath().split("/"); 
        mImage = new File(dir + File.separator + paths[2] + Constants.JPG); // resoleve the file name 
       } catch (Exception e) { // the external storage not available... 
        Log.e(TAG, "create dir error!", e); 
        return; 
       } 
       try { 
        if (mImage.length() > 10) { // > 0 means the file exists 
         // the file exists, done. 
         mIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mImage)); 
         mSaved = true; 
         return; 
        } 
        os = new FileOutputStream(mImage); 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); 
        mIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mImage)); 
        mSaved = true; 
       } catch (FileNotFoundException e) { 
        Log.e(TAG, "io error!", e); 
       } finally { 
        if (os != null) { 
         try { 
          os.close(); 
         } catch (IOException e) { 
          Log.e(TAG, "io closing error!", e); 
         } 
        } 
       } 
      } 
     }).start(); 
     mFantasy.setImageBitmap(bitmap); 
    } 
@Override 
    public void onBitmapFailed(Drawable errorDrawable) { 
     mFantasy.setImageDrawable(errorDrawable); 
    } 

    @Override 
    public void onPrepareLoad(Drawable placeHolderDrawable) { 
     if (placeHolderDrawable != null) { 
      mFantasy.setImageDrawable(placeHolderDrawable); 
     } 
    } 
}; 

@Override 
public void onPrepareOptionsMenu(Menu menu) { 
    mShare.setEnabled(mSaved); 
} 

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    inflater.inflate(R.menu.fantasy, menu); 
    mShare = menu.findItem(R.id.action_share); 
    mShareActionProvider = (ShareActionProvider) mShare.getActionProvider(); 
    mShare.setActionProvider(mShareActionProvider); 

    mShareActionProvider.setShareIntent(mIntent); 
} 

最后,调用Picasso.with(getActivity()).load(mUrl).into(target);

当文件被保存时,用户可以点击共享菜单做分享。

+1

它似乎可以工作。但在我看来,这是不好的报价。文件正在保存某处。 lib应该给他们开发者。这将是一件好事,而不是增加数百万个功能给毕加索库,增加了非常简单和必要的功能。正因为如此,我放弃了使用毕加索。它具有数百万垃圾功能和非常有限的不错功能 – Adem

0

我刚刚发现了一个非常好的解决方案本指南。

https://guides.codepath.com/android/Sharing-Content-with-Intents

的代码将是这样的:

// Can be triggered by a view event such as a button press 
public void onShareItem(View v) { 
    // Get access to bitmap image from view 
    ImageView ivImage = (ImageView) findViewById(R.id.ivResult); 
    // Get access to the URI for the bitmap 
    Uri bmpUri = getLocalBitmapUri(ivImage); 
    if (bmpUri != null) { 
     // Construct a ShareIntent with link to image 
     Intent shareIntent = new Intent(); 
     shareIntent.setAction(Intent.ACTION_SEND); 
     shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); 
     shareIntent.setType("image/*"); 
     // Launch sharing dialog for image 
     startActivity(Intent.createChooser(shareIntent, "Share Image"));  
    } else { 
     // ...sharing failed, handle error 
    } 
} 

// Returns the URI path to the Bitmap displayed in specified ImageView 
public Uri getLocalBitmapUri(ImageView imageView) { 
    // Extract Bitmap from ImageView drawable 
    Drawable drawable = imageView.getDrawable(); 
    Bitmap bmp = null; 
    if (drawable instanceof BitmapDrawable){ 
     bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); 
    } else { 
     return null; 
    } 
    // Store image to default external storage directory 
    Uri bmpUri = null; 
    try { 
     File file = new File(Environment.getExternalStoragePublicDirectory( 
      Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png"); 
     file.getParentFile().mkdirs(); 
     FileOutputStream out = new FileOutputStream(file); 
     bmp.compress(Bitmap.CompressFormat.PNG, 90, out); 
     out.close(); 
     bmpUri = Uri.fromFile(file); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return bmpUri; 
} 

它基本上包括在检索从ImageView的位图,并将其保存到本地临时文件,然后用它来共享。我测试过了,它似乎工作正常。

+1

该解决方案重新压缩位图,浪费内存并损失图像质量 –

+0

是的,它压缩图像。如果它不是你的意图(当时是我的意图),应该很容易删除。 – CarlosRivin