如何使用 Intent.ACTION_SEND 共享 xml 布局
How to share xml layout using Intent.ACTION_SEND
我到处搜索,但找不到解决方案...
我有两个 xml 布局供分享。
我已经膨胀并正确分配给视图。
shareLayout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.share_every_for_self_game_layout, null);
contentView = (LinearLayout) shareLayout.findViewById(R.id.share_container_layout_id);
问题是分享的图片总是空的。
这是我的代码
private void shareResultToFacebook(){
try {
Bitmap bitmap = getBitmapFromView(contentView);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, getImageUri(this, bitmap));
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
}catch (Exception e){
e.getMessage();
}
}
//create bitmap from view and returns it
private Bitmap getBitmapFromView(View view) {
try {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(800, 600, 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;
}catch (Exception e){
Global.logError("getBitmapFromView", e);
}
return null;
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(),
inImage, "", "");
return Uri.parse(path);
}catch (Exception e){
e.getMessage();
}
return null;
}
我找到了解决方案
我已经更改了我的 getBitmapFromView 方法体
private Bitmap getBitmapFromView(LinearLayout view) {
try {
view.setDrawingCacheEnabled(true);
view.measure(View.MeasureSpec.makeMeasureSpec(800, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(600, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap returnedBitmap = Bitmap.createBitmap(view.getDrawingCache());
//Define a bitmap with the same size as the view
view.setDrawingCacheEnabled(false);
return returnedBitmap;
}catch (Exception e){
Global.logError("getBitmapFromView", e);
}
return null;
}
一切正常 ;)
private Bitmap getBitmapFromView(LinearLayout view) {
try {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(800, 600, 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.RED);
}
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}catch (Exception e){
// Global.logError("getBitmapFromView", e);
}
return null;
}
我到处搜索,但找不到解决方案...
我有两个 xml 布局供分享。 我已经膨胀并正确分配给视图。
shareLayout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.share_every_for_self_game_layout, null);
contentView = (LinearLayout) shareLayout.findViewById(R.id.share_container_layout_id);
问题是分享的图片总是空的。
这是我的代码
private void shareResultToFacebook(){
try {
Bitmap bitmap = getBitmapFromView(contentView);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, getImageUri(this, bitmap));
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
}catch (Exception e){
e.getMessage();
}
}
//create bitmap from view and returns it
private Bitmap getBitmapFromView(View view) {
try {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(800, 600, 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;
}catch (Exception e){
Global.logError("getBitmapFromView", e);
}
return null;
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(),
inImage, "", "");
return Uri.parse(path);
}catch (Exception e){
e.getMessage();
}
return null;
}
我找到了解决方案
我已经更改了我的 getBitmapFromView 方法体
private Bitmap getBitmapFromView(LinearLayout view) {
try {
view.setDrawingCacheEnabled(true);
view.measure(View.MeasureSpec.makeMeasureSpec(800, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(600, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap returnedBitmap = Bitmap.createBitmap(view.getDrawingCache());
//Define a bitmap with the same size as the view
view.setDrawingCacheEnabled(false);
return returnedBitmap;
}catch (Exception e){
Global.logError("getBitmapFromView", e);
}
return null;
}
一切正常 ;)
private Bitmap getBitmapFromView(LinearLayout view) {
try {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(800, 600, 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.RED);
}
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}catch (Exception e){
// Global.logError("getBitmapFromView", e);
}
return null;
}