我正在从图库中选择一个图像并移动到MainActivity以显示所选图像。我需要在图库中单击图像并显示它3秒钟后才能在图库屏幕上显示进度对话框然后我应该移动到MainActivity来显示所选择的图像。我在这里通过第一个活动而不是通过画廊屏幕获得对话框。我应该如何实现这个目标?我花了很多时间来解决这个问题。但是我没有得到它。请帮助我。我在这里提供更新的示例代码。
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();
}
}
}
我的MainActivity是
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);
}
}