记录SimpleDraweeView拍照后 加载显示所拍图片,重复显示第一张照片的问题

问题描述:

    先拍照,使用SimpleDraweeView显示照片的缩略图,但是遇到一个问题,就是重新拍照显示的时候,都只是显示拍的第一张照片的缩略图。试了修改了好多东西,最后发现 每次图片 的名字都不一样的话,才能显示不同的图。

代码如下:

一、xml:

<com.facebook.drawee.view.SimpleDraweeView
                    android:id="@+id/photo_cashDeskPhoto"
                    android:layout_width="46dp"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="5dp"
                    android:layout_marginTop="5dp"
                    app:failureImage="@mipmap/showphotofailure"//联网图片重复加载4次失败后显示的图片,但是我是加载本地图片,图片uri不对,显示
                    app:failureImageScaleType="fitCenter"
                    app:placeholderImage="@mipmap/photo_default"//默认显示图片
                    app:placeholderImageScaleType="fitCenter"//默认显示图片布局
                    app:progressBarAutoRotateInterval="1000"//加载进度图转动间隔
                    app:progressBarImage="@mipmap/loading"//加载进度图
                    app:progressBarImageScaleType="fitCenter"/>

二、Java代码:

1、调用相机拍照,并保存照片到本地相册

    private static int photo_index = 0;
 String cameraPath = Environment.getExternalStorageDirectory() + File.separator +
            Environment.DIRECTORY_DCIM + File.separator + "Camera" + File.separator;//系统相册路径
private void capture() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        String filePath = cameraPath + File.separator + "huangheBank";//照片保存文件夹
        File localFile = new File(filePath);
        if (!localFile.exists()) {
            localFile.mkdir();
        }

        //在生成图片文件的时候,不使用固定的 图片名,这样每次加载的缩略图就是对应新拍照片的
        File file = new File(filePath, "cashdesk_" + photo_index + ".jpg"); 
        //File file = new File(filePath, "cashdesk.jpg");

        uri = Uri.fromFile(file);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(intent, IntentCode.REQUEST_CODE_IMAGE);
    }

2、拍照结果:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
       
        //1、这里是为了解决题目问题的第一个尝试,想先把simpleDraweeView的图片资源设置成默认的,然后再设置成刚拍的,但是没用
        // photoCashDeskPhoto.setImageResource(R.mipmap.hhgl_23);


         if (requestCode == IntentCode.REQUEST_CODE_IMAGE && resultCode == RESULT_OK) {
         

 //2、这里是为了解决题目问题的第二个尝试,想设置controller,通过setTapToRetryEnable属性来让图片重新加载,但是也不行,这个是属性 
    //是在加载网络图片时候,点击控件可以重新加载图片的属性,我点击控件拍照,然后加载本地照片,这个属性没用
 //            DraweeController draweeController = Fresco.newDraweeControllerBuilder()
//                    .setUri(uri)
//                    .setTapToRetryEnabled(true)//点击重试是否开启
//                    .setOldController(photoCashDeskPhoto.getController())
//                    .build();
//            photoCashDeskPhoto.setController(draweeController);


            //设置显示的图片uri
            photoCashDeskPhoto.setImageURI(uri);

            

            //保存图片后发送广播通知更新数据库,从而使图片在相册刷新显示出来
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
           
            //3、让下次拍的图片名字改变,这样下次控件加载照片的时候,加载的就是最新拍照片的缩略图 
            //我根据这个结果得到的原因是,如果使用固定的图片名字,系统生成的缩略图不会更新,所以控件显示的缩略图也不变 
            //但是真实的原因过程,可能要从simpleDraweeView的源码来看了。
            photo_index++;

          
        }
    }
 

猜你喜欢

转载自blog.csdn.net/hpp_1225/article/details/80961634
今日推荐