2014-02-28 28 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