0

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

}

nadeem gc
  • 484
  • 1
  • 8
  • 22

3 Answers3

1

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);
}
    }
Talha
  • 12,673
  • 5
  • 49
  • 68
  • thanks . where to run this asynTask class?? Should I call execute() method to do this using asynTask? – nadeem gc Nov 14 '12 at 15:51
  • How to run this class? I am new to android , and I don't know how to use AsynTask. So, please guide me. Thanks – nadeem gc Nov 14 '12 at 15:54
  • I wrote below how you can use asycTask , for more information refer this url; http://developer.android.com/reference/android/os/AsyncTask.html – Talha Nov 14 '12 at 21:09
  • It worked. Thanks. I will remember to use AsyncTask in future for this type of work. Thanks again – nadeem gc Nov 15 '12 at 04:30
1
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();

      }
}
Talha
  • 12,673
  • 5
  • 49
  • 68
0

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

Community
  • 1
  • 1
Axel
  • 685
  • 5
  • 14