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

时间:2016-01-23 06:50:35

标签: android

我打开相机和图库的弹出窗口,用于拍摄图像并在图像视图中显示,但不会立即显示在图像视图中。从相机图像捕获图像后,不会立即显示在图像视图中从相机返回后弹出窗口也没有关闭。第二次回到相同的图像时显示。

这是我的代码。

 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();
                    }
                });
            }
        });

这是我的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);
        }
    }

谢谢!!!

1 个答案:

答案 0 :(得分:0)

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