2014-03-28 163 views
0

我有一張照片可以更改。 我可以選擇我的畫廊中的圖片或拍攝照片。 當我拍攝照片時,我裁剪圖像。 但應用程序需要時間來裁剪我的照片,所以我想顯示一個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; 
     } 
    }; 

} 
+0

與我們分享你的努力! –

+0

爲此你必須告訴我們你的代碼。 – 2014-03-28 11:10:56

+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 
+0

這是一樣的。 當crop的畫面接近時,我會看到progressDialog。 – MichelRobico