How to load Progress Bar Circle after user selects image from gallery in android?

纵饮孤独 提交于 2020-01-03 04:46:06

问题


I am doing an app in which user selects an image from gallery and goes to second activity with the selected image from gallery and displays it in second activity.But it takes some time(approx 3 sec) to go to second activity after user clicks on an image in gallery.I want to display progress bar circle for that much of time after the user selects an image from gallery and want to make progress bar circle invisible when execution moves to second activity.I am not getting any idea how to do my task?Should I use any AsyncTask?Please help me.I am stuck here. I am providing my sample code.

My first Activity is

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



 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, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)     {
      Uri selectedImage = data.getData();    
     ProgressDialog progress=new ProgressDialog(getApplicationContext());
     progress.setIndeterminate(true);
     progress.setTitle("Please wait");
     progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
     progress.show();


      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();

      progress.dismiss();

      Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
      intent.putExtra("path", picturePath); 
      startActivity(intent); 
     }
 }
}

my first activity xml

  <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"
 >
  <ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:src="@drawable/homepage"
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentBottom="true" 
    android:scaleType="fitXY"/>

  <ImageButton
    android:id="@+id/select_photo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="126dp"
    android:background="@android:color/transparent"
    android:src="@drawable/select_photo" />

  </RelativeLayout>

my second activity 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);
}

  }

My second activity xml is

  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.progressbarcircle.MainActivity"
  tools:ignore="MergeRootFrame" >

  <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
    android:layout_gravity="center" />

  </FrameLayout>

Thanks in advance.please help me.


回答1:


If your first Activity is taking too much time create a ProgressDialog object and show() it after

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)    {
  Uri selectedImage = data.getData();          

and dismiss() it before startActivity(intent);

You can do the same for the second Activity if it takes too long.



来源:https://stackoverflow.com/questions/24036184/how-to-load-progress-bar-circle-after-user-selects-image-from-gallery-in-android

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