Bitmap created from FrameLayout is black

纵然是瞬间 提交于 2020-01-15 10:36:09

问题


I am trying to create a bitmap for share intent.Bitmap is created( I hope because it is not null which it was when tried out buildDrawingCache() solution) but when it is shared it is null.I have looked at other SO answers also but none of them works. In my code you can see I have tried other solutions also but created bitmap is null there.Here at least the bitmap is created but it's black. My image is 500px X 500px image by the way if it matters.

share_image.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/msg_bg"/>
        <TextView
            android:id="@+id/msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:padding="35dp"
            android:textColor="@android:color/black"
            android:layout_gravity="center_vertical|center_horizontal"/>
    </FrameLayout>
</LinearLayout>

Here is where I am trying to create the bitmap and later one is the function which compresses the bitmap to PNG and invokes the share intent.

    View image=inflater.inflate(R.layout.share_image,null,false);
                                        FrameLayout frame=(FrameLayout)image.findViewById(R.id.frame);
                                        TextView text=(TextView)image.findViewById(R.id.msg);
                                        text.setText(msg.getMessage());

//                                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(500, 500);
//                                        frame.setLayoutParams(layoutParams);
//                                        frame.setDrawingCacheEnabled(true);
//                                       frame.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
//                                                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//                                        frame.layout(0, 0, frame.getMeasuredWidth(), frame.getMeasuredHeight());
//                                        frame.buildDrawingCache();
//                                        Bitmap bm=frame.getDrawingCache();
//                                        frame.destroyDrawingCache();

                                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(500, 500);
                                        frame.setLayoutParams(layoutParams);
                                        Bitmap b = Bitmap.createBitmap( frame.getLayoutParams().width, frame.getLayoutParams().height, Bitmap.Config.ARGB_8888);
                                        Bitmap finalBitmap=b.copy(Bitmap.Config.ARGB_8888, true);
                                        Canvas c = new Canvas(finalBitmap);
                                        frame.layout(0, 0, frame.getLayoutParams().width, frame.getLayoutParams().height);
                                        frame.draw(c);

//                                        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.msg_bg);
//                                        Bitmap finalBitmap=bitmap.copy(Bitmap.Config.ARGB_8888, true);
//                                        Canvas canvas = new Canvas(finalBitmap);
//                                        Paint paint = new Paint();
//                                        paint.setColor(Color.BLACK);
//                                        paint.setTextSize(48f);
//                                        Rect bounds = new Rect();
//                                        paint.getTextBounds(msg.getMessage(), 0, msg.getMessage().length(), bounds);
//                                        int x = (finalBitmap.getWidth() - bounds.width())/2;
//                                        int y = (finalBitmap.getHeight() + bounds.height())/2;
//                                        paint.setTextSize(48f*500/bounds.width());
//                                        canvas.drawText(msg.getMessage(), x, y, paint);
                                        shareBitmap(finalBitmap);



private void shareBitmap (Bitmap bitmap) {
        try {
            File file = new File(getActivity().getCacheDir(),"pk.png");
            FileOutputStream fOut = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
            file.setReadable(true, false);
            final Intent intent = new Intent(     android.content.Intent.ACTION_SEND);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            intent.setType("image/png");
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

来源:https://stackoverflow.com/questions/45844870/bitmap-created-from-framelayout-is-black

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!