How to display a dialog over gallery screen in android?

流过昼夜 提交于 2019-12-23 04:05:50

问题


I am selecting an image from gallery and moving to MainActivity to display the image selected.I need to display a progress dialog over gallery screen only after I click on an image from gallery and display it for 3 seconds and then i should move to MainActivity to display the selected image.I am getting the dialog here over first activity not over the gallery screen.How should i achieve this?I have spent so much time to solve this.But i did not get it.Please help me .I am providing my updated sample code here.

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;




public class LauncherActivity extends Activity 
{
 private static int RESULT_LOAD_IMAGE = 2;
 ImageButton gallery;
 Bitmap bitmap_for_gallery;
 String picturePath;
 ProgressDialog dialog;;
 Uri selectedImage;

 protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.launcher);
gallery = (ImageButton)findViewById(R.id.select_photo);

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, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
      if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {

      AsyncTask<String, Void, String> updateTask = new AsyncTask<String, Void, String>(){


            @Override

            protected void onPreExecute() {
              selectedImage = data.getData();
                dialog = new ProgressDialog(LauncherActivity.this);
                dialog.setMessage("Loading...");
                dialog.show();
            }
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub
                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();
                return null;
            }
            protected void onPostExecute(String result) {
                Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
                intent.putExtra("path", picturePath); 
                startActivity(intent);
                dialog.dismiss();
            }

        };

        updateTask.execute();
  }
 }
}

and my MainActivity is

 import android.app.Activity;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.os.Bundle;
 import android.widget.FrameLayout;
 import android.widget.ImageView;
 import android.widget.RelativeLayout.LayoutParams;


 public class MainActivity extends Activity {
 ImageView background;
 Bitmap transfered;
  FrameLayout.LayoutParams layoutParams;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 background=(ImageView)findViewById(R.id.imageView1);

 layoutParams=new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
 Bundle extras = getIntent().getExtras();

    String picturePath=extras.getString("path");
     transfered=BitmapFactory.decodeFile(picturePath);  
 background.setImageBitmap(transfered); 
 background.setAdjustViewBounds(true);
 background.setLayoutParams(layoutParams);
 }

}

来源:https://stackoverflow.com/questions/24060623/how-to-display-a-dialog-over-gallery-screen-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!