首页 > 解决方案 > 分享意图后如何重新加载片段?

问题描述

我有三个片段,在其中一个片段中,我有许多由不同图像加载的图像视图。我有一个分享按钮,用于使用 Canvas 分享所有图像视图的组合。共享功能非常适合第一次加载。当我们再次点击分享按钮时,应用程序崩溃了。

当共享完成/取消时,我想重新加载片段。

有没有办法做到这一点?

这是加载片段的方式

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    final View view1 = inflater.inflate(R.layout.fragment_three,container,false);
    View view = inflater.inflate(R.layout.fragment_three,container,false);
    return view;
}

public void onViewCreated(View rootView, Bundle savedInstanceState){
        super.onViewCreated(rootView, savedInstanceState);
        final View v = rootView;
        final Context context = null;
...................

}

抱歉,没有空间容纳所有代码

这是用于在视图内部共享的代码

    img = (ImageView)getView(). findViewById(R.id.imageView15);

    collageImage = (ImageView) getView().findViewById(R.id.imageViewcam);
        collageImage.destroyDrawingCache();
        collageImage.buildDrawingCache();
        collageImage.getDrawingCache();
        collageImage.setImageResource(android.R.color.transparent);

       mBackgroundImage = Bitmap.createBitmap(450,350, Bitmap.Config.ARGB_8888);
       final Canvas canvas = new Canvas(mBackgroundImage);


img.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ImageView img1 = null;

                img1 = (ImageView)getView(). findViewById(R.id.imageViewcam);
                img1.destroyDrawingCache();
                img1.setDrawingCacheEnabled(true);

                img1.buildDrawingCache();
                Bitmap bmap1=null;
                bmap1 = img1.getDrawingCache();


           ImageView img3 = (ImageView)getView(). findViewById(R.id.imageView3);
               img3.destroyDrawingCache();
               img3.buildDrawingCache();
               Bitmap bmap3 = img3.getDrawingCache();

        Canvas canvas = new Canvas(bmap1);
                canvas.drawBitmap(bmap1, 0f, 0f, null);

        float xo51 = img3.getX(); // x-coordinate
                float yo51 = img3.getY();
                canvas.drawBitmap(bmap3, xo51, yo51, null);

        collageImage.setImageBitmap(bmap1);
                Drawable mDrawable = collageImage.getDrawable();

        Bitmap bitmap = ((BitmapDrawable)mDrawable).getBitmap();

if (Build.VERSION.SDK_INT >= 23)
                {
                    if (checkPermission())
                    {

                        try {
                            File file = new File(getActivity().getExternalCacheDir(),"logicchip.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.createChooser(intent, "Share image via"));

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    } else {
                        requestPermission(); // Code for permission
                    }
                }
                else
                {


                }

        }
        });
    }

我正在寻找的是避免共享错误

错误:画布:试图在 android 中使用回收的位图 android.graphics.Bitmap

它发生在 img1.buildDrawingCache();

或重新加载整个片段。

当我们激活 fragment1 时,第三个 Fragment 会重新加载。

希望清楚..

任何想法 ?

谢谢

标签: android

解决方案


试试这个进行片段调用..

getSupportFragmentManager().beginTransaction().replace(R.id.activity_main,fragment).commit();

推荐阅读