Android 如何在共享意图之后重新加载片段?

Android 如何在共享意图之后重新加载片段?,android,Android,我有三个片段,在其中一个片段中,我有许多由不同图像加载的ImageView。我有一个共享按钮,用于使用画布共享所有ImageView的组合。共享功能在第一次加载时可以完美地工作。当我们再次单击共享按钮时,应用程序崩溃 我想在共享完成/取消时重新加载片段 有没有办法做到这一点 这就是片段的加载方式 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,

我有三个片段,在其中一个片段中,我有许多由不同图像加载的ImageView。我有一个共享按钮,用于使用画布共享所有ImageView的组合。共享功能在第一次加载时可以完美地工作。当我们再次单击共享按钮时,应用程序崩溃

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

有没有办法做到这一点

这就是片段的加载方式

@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时,第三个片段被重新加载

希望天气晴朗

有什么想法吗


谢谢

此代码将帮助您

//用于重新加载片段

getFragmentManager().beginTransaction().replace(R.id.hundler,Main_Fragment()).commit()

为片段调用尝试此方法

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

非常感谢。当共享意图完成时,我需要使用它。有什么办法可以检测到吗?。我尝试了您的代码,但出现语法错误,并在共享意图后替换为“getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.main,getTargetFragment()).commit();”。应用程序崩溃。。但共享窗口仍在那里…谢谢,但崩溃了。。im将所有imageview合并为位图,用于共享。它只工作了一次。当我们第二次共享时,应用程序崩溃。所以我需要在意图完成后重新加载片段以避免第二次共享…有什么想法吗??