大佬教程收集整理的这篇文章主要介绍了android – 如果方向改变,相机不会覆盖旧图像,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@Override public void onClick(View v) { final FragmentManager fm = ((MyActivity) getContext()).getSupportFragmentManager(); Fragment auxiliary = new Fragment() { @Override public void onActivityResult(int requestCode,int resultCode,Intent data) { resizeResaveAndDisplayPhoto(); super.onActivityResult(requestCode,resultCode,data); fm.begintransaction().remove(this).commit(); } }; fm.begintransaction().add(auxiliary,"FRAGMENT_TAG").commit(); fm.executePendingtransactions(); Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (null != takePictureIntent.resolveActivity(view.getContext().getPackageManager())) { ((MyActivity)view.getContext()).setrequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTraiT); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFilE)); auxFragment.startActivityForResult(takePictureIntent,Constants.requEST_CODE_PHOTO); } }
调用resizeResaveAndDisplayPhoto时,它会执行以下AsyncTask
public static class ResizeThenLoadImageTask extends AsyncTask<String,Void,Bitmap> { private final WeakReference<ImageView> imageViewWeakReference; private final WeakReference<File> fileWeakReference; private final WeakReference<Context> weakContext; private final int reqHeight; private final int reqWidth; public ResizeThenLoadImageTask(Context context,ImageView imageView,File file,int reqHeight,int reqWidth) { weakContext = new WeakReference<Context>(context); imageViewWeakReference = new WeakReference<>(imageView); fileWeakReference = new WeakReference(filE); this.reqHeight = reqHeight; this.reqWidth = reqWidth; } @Override public Bitmap doInBACkground(String... params) { File file = fileWeakReference.get(); Bitmap bitmap = null; if (null != filE) { bitmap = ImageUtils.reduceImageSize(file,reqHeight,reqWidth); ImageUtils.saveBitmapToGivenFile(bitmap,filE); } return bitmap; } @Override public void onPostExecute(Bitmap bitmap) { if (null != imageViewWeakReference && null != fileWeakReferencE) { ImageView imageView = imageViewWeakReference.get(); File file = fileWeakReference.get(); if (null != imageView) { if (null != bitmap) { imageView.setImageBitmap(bitmap); } else { imageView.setImageresource(R.drawable.photo); } imageView.postDelayed(new Runnable() { @Override public void run() { if (null != weakContext.get()) { ((MyActivity) weakContext.get()).setrequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); } } },10000); } } } }
您可能会注意到我在拍照前锁定了方向,并在显示照片10秒后将其解锁.这个技巧是我的故障排除的一部分.所以情况就是这样.
上述系统工作得很好.在下列情况下会出现问题
>假设我已经在ImageView中有一张照片但想要替换它.
>所以我点击ImageView拍摄一张新照片.
>如果我旋转设备拍摄新照片,那么当我返回新照片时,会在旧照片返回之前短暂显示.
>所以我锁定方向,看看发生了什么.这是我发现的.
>只要我锁定方向,新照片就会显示.一旦方向解锁(10秒),旧照片就会返回.
>如果我离开活动和返回,旧照片仍在显示.
>如果我完全关闭应用程序然后返回,那么我会看到新照片.
以上是大佬教程为你收集整理的android – 如果方向改变,相机不会覆盖旧图像全部内容,希望文章能够帮你解决android – 如果方向改变,相机不会覆盖旧图像所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。