我想将布局分享为图像。下面列出的代码适用于保存操作按钮但不适用于共享操作按钮。共享操作按钮仍然没有响应。我无法弄清楚出了什么问题。
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.eid_card_final, menu);
MenuItem item = menu.findItem(R.id.action_share);
myshare = (ShareActionProvider)item.getActionProvider();
String sharestate = Environment.getExternalStorageState();
RelativeLayout rlShareLayout = (RelativeLayout)findViewById(R.id.relativeLayout);
Bitmap sharebitmap = Bitmap.createBitmap(rlShareLayout.getWidth(), rlShareLayout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas sharecanvas = new Canvas(sharebitmap);
rlShareLayout.draw(sharecanvas);
try{
if (Environment.MEDIA_MOUNTED.equals(sharestate)) {
Random sharerand = new Random();
int sharerandnum = sharerand.nextInt(1000);
String sharefilename = sharerandnum + ".jpeg";
sharef = new File(getExternalFilesDir(null),sharefilename);
FileOutputStream shareout = new FileOutputStream(sharef);
boolean success = sharebitmap.compress(CompressFormat.PNG, 100, shareout);
}
} catch (Exception e) {
e.printStackTrace();
}
Intent shareintent = new Intent(Intent.ACTION_SEND);
shareintent.setType("image/*");
Uri shareuri = Uri.fromFile(sharef);
shareintent.putExtra(Intent.EXTRA_STREAM, shareuri.toString());
shareintent.setData(shareuri);
myshare.setShareIntent(shareintent);
return true;
}
我使用相同的代码(代码的意图部分除外)来保存文件并且它工作得非常好,奇怪的是同样的代码应该至少在创建选项菜单时自动保存文件(如果不是使用ACTION_SEND发送) / loaded,但保存文件的代码也不起作用(以及代码的意图部分),最糟糕的是没有错误。我被困了。请指导出错的地方