捕获图像后更新图库 [英] Update Gallery after capture an image android

查看:84
本文介绍了捕获图像后更新图库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个可以从相机捕获图像并根据图像路径显示结果的应用.在用于捕获图像的按钮内,如下所示

I try to create an app that can capture image from camera and show the result based on image path. Inside the button which handles capturing image as below

btnImg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                file = new File(Environment.getExternalStorageDirectory() + File.separator + "Nama_foto_" + waktu + ".jpg");

                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                intent.putExtra("return-data", true);

                getParentFragment().startActivityForResult(intent, CAPTURE_IMAGE);
            }catch (ActivityNotFoundException anfe){
                //display an error message
                String errorMessage = "Whoops - your device doesn't support capturing images!";
                Toast toast = Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    });

作为摄像头捕捉的回应,我在下面的代码中编写了

And as a respon from camera capturing Im writing below code

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if(requestCode == CAPTURE_IMAGE) {

            performCrop();
        }
        else if(requestCode == PIC_CROP){
            //get the returned data
            Bundle extras = data.getExtras();
            //get the cropped bitmap
            Bitmap thePic = extras.getParcelable("data");

            picturePath = file.getAbsolutePath();

            try {
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                thePic.compress(Bitmap.CompressFormat.JPEG, 80, bytes);

                file.createNewFile();
                FileOutputStream out = new FileOutputStream(file);
                out.write(bytes.toByteArray());
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            //retrieve a reference to the ImageView
            //ImageView picView = (ImageView)findViewById(R.id.picture);
            //display the returned cropped image
        imgView.setImageBitmap(thePic);
        }

        try {

        }catch (Exception e) {
            Toast.makeText(getActivity(), "Maaf gagal mengambil foto.", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
}

}

在OnCreateView外部声明为空字符串的变量picturePath. 问题是,每当我运行应用程序并捕获图像时.该图像没有立即显示在图库中,但我需要重新运行该应用程序(通过android studio)才能在图库中查看该图像.似乎oncreateview中的某些事件触发了该事件.但是我仍然不知道是什么.

Variable picturePath declared outside OnCreateView as an empty string. The problem is, whenever I run the app and capture the image. The image didnt show immediately in the gallery but instead I need to rerun the app (thru android studio) to see the image in gallery. It seems like something inside oncreateview trigger the event. But I still cant figure out what.

请帮助我解决此问题.谢谢

Pls help me fix the issue. Thanks

推荐答案

似乎您的问题应以不同的方式改写: 这可能会解决您的问题

it seems that your question should be rephrased differently: this may solve your problem

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myNewFile)));

参考: 如何在拍摄照片后更新Android Gallery?/a>

reference : How can I update the Android Gallery after a photo?

这篇关于捕获图像后更新图库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆