2015-10-19 110 views
7

在我的android應用程序中,我有一個位圖(比如b)和一個按鈕。現在,當我點擊按鈕時,我想分享位圖。我利用下面的代碼我onClick()內實現這一目標: -通過Android共享位圖意圖

Intent intent = new Intent(Intent.ACTION_SEND); 
intent.setType("image/png"); 
intent.putExtra(Intent.EXTRA_STREAM, b); 
startActivity(Intent.createChooser(intent , "Share")); 

我期待它能夠處理這個意圖,但我什麼也沒得到所有的應用程序的列表。沒有應用程序的列表,也沒有在android工作室的任何錯誤。我的應用程序剛掛了一段時間,然後退出。

我檢查了位圖,它很好(它不爲空)。

我在哪裏出錯了?

回答

4

引用the documentation

內容:URI保持與意圖相關聯的數據,與ACTION_SEND用於提供數據的一個流被髮送。

b,因此,不應該是一個Bitmap,而是一個Uri指向一個Bitmap,由ContentProvider服務。例如,您可以將Bitmap寫入文件,然後使用FileProvider來提供。

11

由於CommonsWare聲明您需要獲取位圖的URI並將其作爲您的Extra來傳遞。

String bitmapPath = Images.Media.insertImage(getContentResolver(), bitmap,"title", null); 
Uri bitmapUri = Uri.parse(bitmapPath); 
... 
intent.putExtra(Intent.EXTRA_STREAM, bitmapUri); 
+0

正常使用 –

6

**終於我得到了溶液**

步驟1: 分享意圖處理塊。這將你的窗口彈出一個包含在你的應用程序列表電話

public void share_bitMap_to_Apps() { 

    Intent i = new Intent(Intent.ACTION_SEND); 

    i.setType("image/*"); 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    /*compress(Bitmap.CompressFormat.PNG, 100, stream); 
    byte[] bytes = stream.toByteArray();*/ 


    i.putExtra(Intent.EXTRA_STREAM, getImageUri(mContext, getBitmapFromView(relative_me_other))); 
    try { 
     startActivity(Intent.createChooser(i, "My Profile ...")); 
    } catch (android.content.ActivityNotFoundException ex) { 

     ex.printStackTrace(); 
    } 


} 

第2步: 轉換視圖爲位圖

public static Bitmap getBitmapFromView(View view) { 
    //Define a bitmap with the same size as the view 
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),  view.getHeight(), Bitmap.Config.ARGB_8888); 
    //Bind a canvas to it 
    Canvas canvas = new Canvas(returnedBitmap); 
    //Get the view's background 
    Drawable bgDrawable = view.getBackground(); 
    if (bgDrawable != null) 
     //has background drawable, then draw it on the canvas 
     bgDrawable.draw(canvas); 
    else 
     //does not have background drawable, then draw white background on the canvas 
     canvas.drawColor(Color.WHITE); 
    // draw the view on the canvas 
    view.draw(canvas); 
    //return the bitmap 
    return returnedBitmap; 
} 

第3步:

從位圖圖像獲取URI

public Uri getImageUri(Context inContext, Bitmap inImage) { 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 

    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); 
    return Uri.parse(path); 
} 
+1

不要忘了要求android.permission.WRITE_EXTERNAL_STORAGE – appsthatmatter

2
ImageButton capture_share = (ImageButton) findViewById(R.id.share); 
capture_share.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 

    String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap,"title", null); 
    Uri bitmapUri = Uri.parse(bitmapPath); 

     Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.setType("image/png"); 
     intent.putExtra(Intent.EXTRA_STREAM, bitmapUri); 
     startActivity(Intent.createChooser(intent, "Share")); 



    } 
}); 
+0

添加一些說明來回答清楚的認識。 @lalit Baghel –