Question

May be same question is encountered to you before, I am sorry for that but I really need to ask this.I am trying to show Progress dialog and then dismissing it But I am not able to do it. I have searched a lot and tried many ways but cant really get through. I am uploading images after picking from gallery. and during upload i want to show the dialog and after uploading dialog should be dismissed here is my code.

public class FaceActivity extends Activity {
 private static int RESULT_LOAD_IMAGE = 1;
 private Button upbtn;
 public Bitmap bm;
 public ByteArrayOutputStream bos;
 public byte[] bitmapdata;
 public String picturePath;
 private ProgressDialog pd;
 private BitmapFactory.Options options;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_face);


    //pd = new ProgressDialog(FaceActivity.this);

    upbtn = (Button) findViewById(R.id.buttonLoadPicture);
    upbtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);



        }
    });
}
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
                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]);
                picturePath = cursor.getString(columnIndex);
                cursor.close();

                options = new BitmapFactory.Options();
             // will results in a much smaller image than the original
                options.inSampleSize = 8;

                upload();



            }

            }



    public void upload(){

   // Here I am showing the dialog 
        pd = ProgressDialog.show(FaceActivity.this, "Please Wait", "Loading...", true, false);
        bm = BitmapFactory.decodeFile(picturePath);
        bos = new ByteArrayOutputStream(); 
        bm.compress(Bitmap.CompressFormat.JPEG, 40 , bos);
        bitmapdata = bos.toByteArray();

        ParseFile file = new ParseFile("pic.jpg", bitmapdata);
        file.saveInBackground();

        ParseObject po = new ParseObject("Images");       
        po.put("Images", file);
        po.saveInBackground();

        ImageView imageView = (ImageView) findViewById(R.id.targetimage);

        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath,options));
// want to dismiss dialog here 
        pd.dismiss();
        Toast.makeText(this, "Image Uploaded Successfully", Toast.LENGTH_LONG).show();
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_face, menu);
    return true;
}

}

Was it helpful?

Solution

try to do that in asyc task.

private class asynUpload extends AsyncTask<String, Void, Integer> {
protected Integer doInBackground(String... params) {
try {
     runOnUiThread(new Runnable() {
            public void run() {
                pd = ProgressDialog.show(FaceActivity.this, "Please Wait", "Loading...", true, false);
        }
       });

bm = BitmapFactory.decodeFile(picturePath);
    bos = new ByteArrayOutputStream(); 
    bm.compress(Bitmap.CompressFormat.JPEG, 40 , bos);
    bitmapdata = bos.toByteArray();

    ParseFile file = new ParseFile("pic.jpg", bitmapdata);
    file.saveInBackground();

    ParseObject po = new ParseObject("Images");       
    po.put("Images", file);
    po.saveInBackground();

    ImageView imageView = (ImageView) findViewById(R.id.targetimage);

    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath,options));

} catch (Exception e) {             
        return 0;
}
return 1;
}

protected void onPostExecute(Integer result) {
     try {
              runOnUiThread(new Runnable() {
        public void run() {
                       pd.dismiss();
        }
       });

    } catch (Exception e) {}

      super.onPostExecute(result);
}
    }

OTHER TIPS

protected void onActivityResult(int requestCode, int resultCode, Intent data) 

{

   super.onActivityResult(requestCode, resultCode, data);


   if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) 

      {

         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]);
         picturePath = cursor.getString(columnIndex);
         cursor.close();

          options = new BitmapFactory.Options();
          // will results in a much smaller image than the original
          options.inSampleSize = 8;

          // use the task here 
          new asynUpload().execute();

      }
}

You can look here for the idea you've been looking for implementing a background process: doInBackground not working in Android fragment

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top