如何在将设备库中的图像加载到imageView时显示进度条

时间:2014-01-09 14:09:14

标签: android android-progressbar

在我的Android应用程序中,我添加了一个从设备库上传图像的选项。它需要一些时间来加载所以我想包括一个进度条。我使用spinner.setVisibility(View.VISIBLE);方法,但它不起作用。我附上了我的代码片段

protected void onActivityResult(int requestCode, int resultCode, 
        Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
        switch(requestCode) { 
            case REQ_CODE_PICK_IMAGE:
                if(resultCode == RESULT_OK){  
                        Uri selectedImage = imageReturnedIntent.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 filePath = cursor.getString(columnIndex);
                        cursor.close();
                        //Convert Bitmap to Byte Array:-
                        spinner = (ProgressBar) img1.findViewById(R.id.loading);
                        spinner.setVisibility(View.VISIBLE);
                        bitmap = BitmapFactory.decodeFile(filePath);
                        img1 = (ImageView) findViewById(R.id.imageView1);
                        img1.setImageBitmap(bitmap);
                        spinner.setVisibility(View.GONE);
                        ByteArrayOutputStream bos=new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
                        img =bos.toByteArray();

           } }  }

xml照片

 <Button
            android:id="@+id/btnAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/shape"
            android:text="Add" />
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
         <ProgressBar
        android:id="@+id/loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone" />

请帮我纠正代码..

2 个答案:

答案 0 :(得分:1)

使用AsyncTask

public class MyTask extends AsyncTask<String, Integer, String> {

private ProgressBar progressBar;

public MyTask( ProgressBar progressBar ) {

    this.progressBar= progressBar;
}

@Override
protected String doInBackground( String... params ) {
    progressBar.setVisibility( View.VISIBLE );
    //do your work
    return "OK";
   }

@Override
protected void onPostExecute( ArrayList<Comment> result ) {
    progressBar.setVisibility( View.GONE );
 }
};

答案 1 :(得分:0)

spinner = (ProgressBar) img1.findViewById(R.id.loading);
spinner.setVisibility(View.VISIBLE);
bitmap = BitmapFactory.decodeFile(filePath);
img1 = (ImageView) findViewById(R.id.imageView1);
img1.setImageBitmap(bitmap);
spinner.setVisibility(View.GONE);

你确定这不起作用吗? 似乎用户没有足够的时间看到UI的差异。 也许spinner.setVisibility(View.VISIBLE)spinner.setVisibility(View.GONE)内的操作真的很快?