裁剪照片时显示ProgressDialog

时间:2014-03-28 11:09:18

标签: android progressdialog

我有一张可以改变的照片。 我可以在我的画廊中选择一张照片或拍摄照片。 当我拍摄照片时,我裁剪图像。 但该应用程序需要时间来裁剪我的照片,所以我想显示progressDialog。

我的progressDialog出现在我的应用程序中,但它被我裁剪照片的屏幕隐藏了。

如何在我裁剪照片的屏幕内显示progressDialog?

抱歉我的英语不好......

我的代码:

public class FragmentMonCompte extends Fragment {

    private ImageView ivAvatar;
    // YOU CAN EDIT THIS TO WHATEVER YOU WANT
    private static final int CAPTURE_PICTURE = 0;
    private static final int SELECT_PICTURE = 1;

    private String selectedImagePath;
    // ADDED
    private String filemanagerstring;

    private Uri mCapturedImageURI;

    ProgressDialog mProgressDialog = null;
    boolean isPDShow;
    Navigation navigation;
    Intent data;
    Bitmap photo;
    Bundle extras;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_compte, container,
                false);

        ivAvatar = (ImageView) rootView.findViewById(R.id.iv_avatar);

        ivAvatar.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                final Navigation navigation = (Navigation) getActivity();

                mProgressDialog = new ProgressDialog(navigation);
                mProgressDialog.setMessage("Opération en cours...");
                mProgressDialog.setTitle("Patientez");

                final CharSequence[] items = {"Prendre une photo",
                        "Choisir une image", "Annuler"};

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        getActivity());

                // set title
                alertDialogBuilder.setTitle("Avatar :");

                // set dialog message
                alertDialogBuilder.setCancelable(false).setItems(items,
                        new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int item) {

                        if (item == 0) {
                            ContentValues values = new ContentValues();
                            values.put(MediaStore.Images.Media.TITLE,
                                    "");
                            mCapturedImageURI = navigation
                                    .getContentResolver()
                                    .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                            values);

                            Intent intent = new Intent(
                                    MediaStore.ACTION_IMAGE_CAPTURE);

                            intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                    mCapturedImageURI);
                            intent.putExtra("crop", "true");
                            intent.putExtra("aspectX", 170);
                            intent.putExtra("aspectY", 170);
                            intent.putExtra("outputX", 5000);
                            intent.putExtra("outputY", 5000);
                            startActivityForResult(Intent
                                    .createChooser(intent,
                                            "Appareil photo"),
                                            CAPTURE_PICTURE);
                        }

                        if (item == 1) {
                            Intent intent = new Intent(
                                    Intent.ACTION_GET_CONTENT);

                            intent.setType("image/*");
                            intent.putExtra("crop", "true");
                            intent.putExtra("aspectX", 170);
                            intent.putExtra("aspectY", 170);
                            intent.putExtra("outputX", 5000);
                            intent.putExtra("outputY", 5000);
                            startActivityForResult(Intent
                                    .createChooser(intent,
                                            "Choisir une application"),
                                            SELECT_PICTURE);
                        }

                        if (item == 2) {
                            dialog.cancel();
                        }

                    }
                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // afficher
                alertDialogBuilder.show();

            }
        });

        return rootView;
    }

    /**
     * Méthode qui permet d'ouvrir la gallery d'images ou l'appareil photo
     * 
     * @param requestCode CAPTURE ou SELECT
     * @param resultCode RESULT_OK or not
     * @param data Intent
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Navigation navigation = (Navigation) getActivity();

        this.navigation = navigation;
        this.data = data;

        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == CAPTURE_PICTURE) {

                isPDShow = true;

                if( mProgressDialog!=null && !mProgressDialog.isShowing()) {
                    mProgressDialog.show();
                }

                new Thread() {
                    public void run() {
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            handler.sendEmptyMessage(0);
                        }

                        start();
                    }
                };

                Bitmap photo = null;
                Bundle extras = data.getExtras();

                new CapturePhotoAsyncTask().execute((Void) null);

            }

            if (requestCode == SELECT_PICTURE) {

                Bundle extras = data.getExtras();
                if (extras != null) {
                    Bitmap photo = extras.getParcelable("data");
                    ivAvatar.setImageBitmap(photo);

                }

            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if( mProgressDialog!=null & mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    /**
     * Méthode qui retourne un bitmap compressé
     * 
     * @param c Context de l'application
     * @param uri Photo capturée
     * @param requiredSize Taille requise
     * @return Photo convertie en Bitmap
     * @throws FileNotFoundException Fichier non trouvé
     */
    public static Bitmap decodeUri(Context c, Uri uri, final int requiredSize)
            throws FileNotFoundException {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri),
                null, o);

        int widthTmp = o.outWidth, heightTmp = o.outHeight;
        int scale = 1;

        while (true) {
            if (widthTmp / 2 < requiredSize || heightTmp / 2 < requiredSize) {
                break;
            }
            widthTmp /= 2;
            heightTmp /= 2;
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(c.getContentResolver()
                .openInputStream(uri), null, o2);
    }

    class CapturePhotoAsyncTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPostExecute(Boolean result) 
          {
              super.onPostExecute(result);

              ivAvatar.setImageBitmap(photo);

              mProgressDialog.dismiss();
          }

        @Override
        protected Boolean doInBackground(Void... params) {
                try {
                    photo = decodeUri(navigation.getApplicationContext(),
                            mCapturedImageURI, 1000);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            return null;
        }

    }//fin AsyncTask

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            new CapturePhotoAsyncTask().execute((Void) null);
            isPDShow = false;
        }
    };

}

1 个答案:

答案 0 :(得分:1)

class CapturePhotoAsyncTask extends AsyncTask<Void, Void, Boolean> {

@Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(FragmentMonCompte.this);
        pDialog.setMessage("Downloading data");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected void onPostExecute(Boolean result) 
      {
          super.onPostExecute(result);

          ivAvatar.setImageBitmap(photo);

          pDialog.dismiss();
      }

    @Override
    protected Boolean doInBackground(Void... params) {
            try {
                photo = decodeUri(navigation.getApplicationContext(),
                        mCapturedImageURI, 1000);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        return null;
    }

}//fin AsyncTask