2015-10-19 86 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 –