简体   繁体   English

从相机捕获图像在Android的ImageView中不显示?

[英]Capture image from camera does not show in ImageView in Android?

I'm opening the popup window for camera and gallery for taking the images and displaying it in Image-view but does not display immediately in Image-view.After capturing the image from camera image does not display immediately in Image-view popup window also not close after come back from camera.Second time when come back to the same image are showing. 我正在打开相机和图库的弹出窗口以拍摄图像并将其显示在图像视图中,但不会立即显示在图像视图中。从相机图像捕获图像后,也不会立即显示在图像视图弹出窗口中从相机回来后没有关闭。第二次显示到同一张图像时。

Here is my code. 这是我的代码。

 imgProfilePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                View popupView = layoutInflater.inflate(R.layout.camera_popup, (ViewGroup) findViewById(R.id.popup_element));
                popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setWidth(720);
                popupWindow.setHeight(350);
                popupWindow.setFocusable(true);
                popupWindow.showAtLocation(popupView, Gravity.BOTTOM, 0, 87);
                popupWindow.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

                Button btnCamera = (Button) popupView.findViewById(R.id.button_Camera);
                Button btnGallery = (Button) popupView.findViewById(R.id.button_Gallery);
                Button btnDismiss = (Button) popupView.findViewById(R.id.btnCancelCamera);

                btnCamera.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
                        String picformat = "IMG_" + 0 + "_" + s.format(new Date()) + ".png";
                        Log.e(" picformat ", " = " + picformat);

                        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        String extr = Environment.getExternalStorageDirectory().toString() + File.separator + "classNKK_ProfilePic";
                        File myPath = new File(extr, picformat);
                        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(myPath));
                        startActivityForResult(cameraIntent, CAMERA_REQUEST);

                        db = dbhelper.getWritableDatabase();
                        db.execSQL("update Inspector set ProfilePICURL = '" + picformat + "' , DownLoadStatus='1' where Inspector_Id = '" + str_UserId + "'");
                        Log.e("Updated  ", " Succesfully !!! ImageName = " + picformat);

                        Log.e("Camera", " Open");
                        editor.putString("ImageProfilePic_FilePath", extr);
                        editor.putString("profile_picformat", picformat);
                        editor.commit();
                        popupWindow.dismiss();


                    }
                });

                btnGallery.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(galleryIntent, PROFILE_GALLERY);
                        Log.e("Gallery open", "");
                    }
                });

                btnDismiss.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
            }
        });

Here is my onActivityResult code. 这是我的onActivityResult代码。

protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PROFILE_CAMERA && resultCode == RESULT_OK)
        {
            Log.e("11", " 1111");

            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            String getImagePath = sharedPreferences.getString("ImageProfilePic_FilePath", "");
            String getProfile_PicFormat = sharedPreferences.getString("profile_picformat", "");
            Log.e("getImagePath ", " = " + getImagePath + " getPicFormat = " + getProfile_PicFormat);
            Log.e("22"," 22222 ");

            sendPostRequest(getProfile_PicFormat);
            Log.e("Profile Pic ", " UpLoaded SuccesFully !!!!!!!!!!!!!! ");

            String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
            String imageUserProfile_path = baseDir + "/classNKK_ProfilePic/" + getProfile_PicFormat;
            Log.e(" ","imageUserProfile_path --> " + getProfile_PicFormat);
            Bitmap bmp = BitmapFactory.decodeFile(imageUserProfile_path);
            imgProfilePic.setImageBitmap(bmp);
            imgProfilePic.setClipToOutline(true);
            Log.e("33"," 3333 ");

        }

        if (requestCode == PROFILE_GALLERY && resultCode == RESULT_OK) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String imgDecodableString = cursor.getString(columnIndex);
            cursor.close();
            Bitmap bmpGallery = BitmapFactory.decodeFile(imgDecodableString);
            imgProfilePic.setImageBitmap(bmpGallery);
        }
    }

Thanks !!! 谢谢 !!!

查看此链接,它将帮助您从相机拍摄图片并设置为imageview http://javaforbegineer.blogspot.in/2016/01/android-camera-tutorial.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM