0

I am selecting an image from gallery in first activity and with the selected image i am going to second activity.Here i want to display an alert dialog showing the message "Loading...." after user selects an image from gallery and before going to the next activity,please help me.

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
 import android.os.CountDownTimer;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;



 public class LauncherActivity extends Activity 
  {

   private static int RESULT_LOAD_IMAGE = 1;
 ImageButton gallery;
 ImageView gallery_image;
 int height = 500;
 Intent i;
 Intent intent;
int width = 380;



@SuppressWarnings("deprecation")
protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.launcher);
gallery = (ImageButton)findViewById(R.id.select_photo);
camera = (ImageButton)findViewById(R.id.take_photo);
free_apps = (ImageButton)findViewById(R.id.free_apps);
gallery.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent gallery_intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(gallery_intent, 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]);
      String picturePath = cursor.getString(columnIndex);
      cursor.close();


        final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setMessage("Loading....");
     final AlertDialog alert = dialog.create();
     alert.show();

    new CountDownTimer(5000, 1000) {

  @Override
  public void onTick(long millisUntilFinished) {
      // TODO Auto-generated method stub

  }

  @Override
  public void onFinish() {
      // TODO Auto-generated method stub

       alert.dismiss();
     }
   }.start();

      Bitmap bitmap_for_gallery =              Bitmap.createScaledBitmap(BitmapFactory.decodeFile(picturePath), width, height, true);
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      bitmap_for_gallery.compress(Bitmap.CompressFormat.PNG, 100, stream);
      byte[] byteArray = stream.toByteArray();
      Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
      intent.putExtra("image", byteArray);
      startActivity(intent);

       }

     }





   }
Lakshmipathi
  • 87
  • 2
  • 9
  • why u want to do that – Shakeeb Ayaz Apr 15 '14 at 10:26
  • Read this: http://stackoverflow.com/questions/2115758/how-to-display-alert-dialog-in-android – joao2fast4u Apr 15 '14 at 10:26
  • even if u show the dialog it will not be visible bcoz it will take be very small processing time to go from one activity to another – Shakeeb Ayaz Apr 15 '14 at 10:28
  • It takes almost 3 sec time to go to other activity.To get the user understand that image is loading,I want to display the alert dialog showing "Loading...." for those 3 seconds time.I will dismiss the dialog after 3 sec. so whenever the user selects the image,at that moment i want to display alert dialog for 3 sec.this is my requirement.I have written the code in onActivityForResult() method.But dialog is not displayed but if i write the same code out of the method,dialog is displayed. – Lakshmipathi Apr 15 '14 at 10:33
  • Post some code. Without a code it is difficult – fecub Apr 15 '14 at 10:58

1 Answers1

0

Well you see, to show a dialog showing the message "Loading....", you need to have the reference of a Running Activity, or more precisely the reference of an activity that is currently visible to the user. For example, if you show this message on Activity A, and then( without canceling this dialog) you start another activity B, then this will give you an error. This is because you used the reference of Activity A to show the dialog, and when you started activity B, Activity A lost its visibility.

In this back drop, I think you are better off converting the file stream to bitmap on Activity A itself. Later save this to a Static Variable. May be, create a class Utills, and let it have following variable

public static Bitmap reusable;

In activity A you initialize "reusable", and finally in activity B you consume it. And after setting "reusable" to the ImageView in Activity B, you can also destroy this memory unit.This can reduce the delay you are having. Moreover, if this delay is when you test the code on a emulator then I think the delay will be reduced on any real device.

Parth Kapoor
  • 1,494
  • 12
  • 23
  • almost same delay is coming.please tell how to display the alert dialog after user selects the image – Lakshmipathi Apr 15 '14 at 10:57
  • after selecting image from gallery ...what is the next action? ok, means with that image what he is going to do ? – GvSharma Apr 15 '14 at 11:37
  • After selecting the image, i am moving to other activity.There i am assigning this image to a bitmap and displaying there. – Lakshmipathi Apr 15 '14 at 12:25
  • Did you get same delay on a real device? Did you test it on a real device? I assume you will not have this problem on a device.. – Parth Kapoor Apr 15 '14 at 12:32