Android - android 中的屏幕旋转和位图错误

Android - Error with screen rotation and bitmap in android

我的应用程序拍摄我的网站图片,图片可以与whatsapp和其他社交网络分享,创建一个文件夹并保存图片,它工作正常,但是当旋转屏幕并点击分享时,我看到这个位图为空。 有没有人有办法解决吗?按照我的代码:

 public void onClick(View v) {
        if( mRecyclerViewOnClickListenerHack == null ) {
            OutputStream fOut = null;
            ImageView imgflag = (SimpleDraweeView) itemView.findViewById(R.id.iv_image);
            imgflag.setDrawingCacheEnabled(true);
            imgflag.buildDrawingCache();
            Bitmap bm=imgflag.getDrawingCache();

            File root = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "ImageTop" + File.separator);
            root.mkdirs();
            File sdImageMainDirectory = new File(root, mList.get(getAdapterPosition()).getFoto());
            Uri imageUri = Uri.fromFile(sdImageMainDirectory);

            try {
            fOut = new FileOutputStream(sdImageMainDirectory);
                if (bm == null){
                Log.d(TAG, "Bitmap is null.");
                }else{
                bm.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
                Log.d(TAG, "Bitmap is ok.");
                }

            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
            sendIntent.setType("image/jpg");
                mContext.startActivity(Intent.createChooser(sendIntent, "Share"));

                fOut.flush();
                fOut.close();
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(mContext, "Error.", Toast.LENGTH_SHORT).show();
            }

        }else{
            mRecyclerViewOnClickListenerHack.onClickListener(v, getAdapterPosition());
        }
    }

已经在清单中添加了configChange,但没有解决方案。 我是 android 和 java 的初学者。 感谢所有能帮助我的人。

onCreate 方法在方向改变时再次执行。所以可能当方向改变时它无法获取图像 uri。所以尝试从包中保存和检索它。

在class中声明Uri p。

在 onCreate 中添加这个

p  =  yourImageUri;   // you image uri

if(savedInstanceState!=null) {
p= Uri.parse(savedInstanceState.getString("imageUri"));
}

并在class中添加此方法。

@Override
protected void onSaveInstanceState(Bundle outState) 
{
outState.putExtra("imageUri", p.toString()); 
}