如果方向改变,相机不会覆盖旧图像

Posted

技术标签:

【中文标题】如果方向改变,相机不会覆盖旧图像【英文标题】:Camera not overriding old image if orientation changes 【发布时间】:2016-06-08 23:34:42 【问题描述】:

我有一个带有许多不同 ViewHolder 的 RecyclerView 适配器。其中一个 ViewHolders 包含一个 ImageView,它需要能够拍照、调整大小、然后显示它。对于模块化,我希望 ViewHolder 是自包含的:它而不是父活动应该处理与照片拍摄和显示过程有关的所有事情。文件路径也是恒定的(它永远不会改变)。其实就是/storage/emulated/0/com.company.app/myst/cat.jpg。因此,这是我对 ImageView 的 onClick 方法的实现。

@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 秒),旧照片就会返回。 如果我离开活动并返回,旧照片仍在显示。 如果我完全关闭应用程序然后返回,那么我会看到新照片。

【问题讨论】:

所以我用三分之一的分数来问这个问题。请给出有意义的答案。 您是仅在方向上遇到此问题,还是一直只显示默认图像? 向我们展示Activity 在方向更改时首次创建或重新创建的代码,您可以将文件中的位图加载到ImageView 中。 【参考方案1】:

当设备旋转时,正在运行的 Activity 以及附加到它的 asyncTask 都会重新创建,这可能会导致泄漏。

您可能需要在服务内部而不是在活动中调用 asyncTask,以便将 asyncTask 附加到服务的生命周期。

【讨论】:

您的回答没有抓住重点。它没有解释为什么文件内容会回滚到上一张照片。请务必阅读整个问题。 以前的语言太混乱了。所以我已经编辑了文本。 我不确定我的回答是否能解决您的问题,是吗?如果是,我将编辑我的答案,试图解释为什么文件内容回滚到上一张照片,如果没有,它没有意义尝试解释它。

以上是关于如果方向改变,相机不会覆盖旧图像的主要内容,如果未能解决你的问题,请参考以下文章

方向改变时自动检测图像

如何实现 Android 相机以任何屏幕方向拍照?

使用覆盖将图像保存到文档目录

Android中Camera方向问题总结

如何设置相机图像方向?

如何设置相机图像方向?