Android 裁剪照片时显示进度对话框
我有一张可以换的照片。 我可以在我的图库中选择一张图片或捕获一张照片。 当我拍摄照片时,我会裁剪图像。 但是应用程序需要时间来裁剪我的照片,所以我想显示一个progressDialog 我的progressDialog出现在我的应用程序上,但它被裁剪照片的屏幕隐藏 如何使progressDialog显示在裁剪照片的屏幕内 对不起,我的英语不好 我的代码:Android 裁剪照片时显示进度对话框,android,progressdialog,Android,Progressdialog,我有一张可以换的照片。 我可以在我的图库中选择一张图片或捕获一张照片。 当我拍摄照片时,我会裁剪图像。 但是应用程序需要时间来裁剪我的照片,所以我想显示一个progressDialog 我的progressDialog出现在我的应用程序上,但它被裁剪照片的屏幕隐藏 如何使progressDialog显示在裁剪照片的屏幕内 对不起,我的英语不好 我的代码: public class FragmentMonCompte extends Fragment { private ImageVie
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;
}
};
}
公共类FragmentMonCompte扩展了Fragment{
私有图像视图ivAvatar;
//您可以将其编辑为您想要的任何内容
私有静态最终整数捕获图片=0;
私有静态最终整数选择_PICTURE=1;
私有字符串selectedImagePath;
//增加
私有字符串filemanagerstring;
私有Uri mCapturedImageURI;
ProgressDialog mProgressDialog=null;
布尔isPDShow;
航海;
意向数据;
位图照片;
捆绑附加;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment_compte,容器,
假);
ivAvatar=(ImageView)rootView.findviewbyd(R.id.iv_avatar);
ivAvatar.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
最终导航导航=(导航)getActivity();
mProgressDialog=新建进度对话框(导航);
设置消息(“操作过程…”);
mProgressDialog.setTitle(“Patientez”);
最终字符序列[]项={“Prendre une照片”,
“Choisir une图像”、“环形器”};
AlertDialog.Builder alertDialogBuilder=新建AlertDialog.Builder(
getActivity());
//定名
alertDialogBuilder.setTitle(“化身:”);
//设置对话框消息
alertDialogBuilder.setCancelable(false).setItems(items,
新建DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口对话框,int项){
如果(项==0){
ContentValues=新的ContentValues();
value.put(MediaStore.Images.Media.TITLE,
"");
mCapturedImageURI=导航
.getContentResolver()
.insert(MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI、,
价值观);
意图=新意图(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_输出,
mCapturedImageURI);
意图。额外(“作物”、“真实”);
putExtra(“aspectX”,170);
意图.putExtra(“aspectY”,170);
意向。额外(“输出”,5000);
意向。额外投入(“产出”,5000);
startActivityForResult(意图
.createChooser(意图,
“装置照片”),
拍摄照片);
}
如果(项目==1){
意图=新意图(
意图、行动(获取内容);
intent.setType(“image/*”);
意图。额外(“作物”、“真实”);
putExtra(“aspectX”,170);
意图.putExtra(“aspectY”,170);
意向。额外(“输出”,5000);
意向。额外投入(“产出”,5000);
startActivityForResult(意图
.createChooser(意图,
“Choiser une申请”),
选择(图片),;
}
如果(项目==2){
dialog.cancel();
}
}
});
//创建警报对话框
AlertDialog AlertDialog=alertDialogBuilder.create();
//阿菲舍尔
alertDialogBuilder.show();
}
});
返回rootView;
}
/**
*米索德·奎·佩米特·奥夫里尔画廊图片和设备照片
*
*@param requestCode捕获ou SELECT
*@param resultCode RESULT\u是否正常
*@param数据意图
*/
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
导航导航=(导航)getActivity();
这个.导航=导航;
这个数据=数据;
if(resultCode==Activity.RESULT\u确定){
if(requestCode==捕获图片){
isPDShow=true;
if(mProgressDialog!=null&&!mProgressDialog.isShowing()){
mProgressDialog.show();
}
新线程(){
公开募捐{
试一试{
睡眠(5000);
}捕捉(中断异常e){
handler.sendEmptyMessage(0);
}
start();
}
};
位图照片=空;
Bundle extras=data.getExtras();
新CapturePhotoAsyncTa
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