Android:即使设置可见,也不会显示进度条。

时间:2015-11-26 15:07:29

标签: android visibility android-progressbar

我正在开发一个Android项目,当用户上传图片时,我想显示一个进度条。但是,即使设置了show进度条,它也无法正常工作。我做错了什么?

代码:

 private ProgressBar spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_restaurant_images);

        spinner=(ProgressBar)findViewById(R.id.progressBar);
        spinner.setVisibility(View.GONE);
    // Rest of the non-relevant to problem code
}

      private void uploadImage(boolean profilePhoto) {
            spinner.setVisibility(View.VISIBLE);
            if (!(reducedSizeBitmap == null)) {
                reducedSizeBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();
                this.restaurantService.addRestaurantImage(byteArray, profilePhoto);
            }
            spinner.setVisibility(View.GONE);

        }

XML文件:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/restaurant_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="top|center"
        android:orientation="vertical">


        <ImageView
            android:id="@+id/userPhotoImageView"
            android:layout_width="match_parent"
            android:layout_height="436dp"
            />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <internetlegion.com.restaurantlunchify.Templates.MaterialRippleLayout
                style="@style/RippleStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:layout_marginTop="54dp"
                android:layout_marginRight="20dp"
                android:layout_marginEnd="20dp"
                app:rippleColor="@color/main_color_grey_400">

                <internetlegion.com.restaurantlunchify.Templates.RobotoTextView
                    android:id="@+id/uploadUserImageButton"
                    style="@style/ButtonStyle.RaisedEmpty"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@color/main_color_900"
                    android:minWidth="112dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginEnd="20dp"
                    android:text="Upload"
                    android:textColor="#fff" />
            </internetlegion.com.restaurantlunchify.Templates.MaterialRippleLayout>

            <CheckBox
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="0dp"
                android:layout_marginRight="0dp"
                android:layout_marginEnd="0dp"
                android:layout_gravity="center_horizontal"
                android:text="Default"
                android:id="@+id/profilePhotoCheckBox"
                android:checked="false" />

            <internetlegion.com.restaurantlunchify.Templates.MaterialRippleLayout
                style="@style/RippleStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:layout_marginTop="54dp"
                app:rippleColor="@color/main_color_grey_400">


            <internetlegion.com.restaurantlunchify.Templates.RobotoTextView
                android:id="@+id/userPhotoButtonSelect"
                style="@style/ButtonStyle.RaisedEmpty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/main_color_900"
                android:minWidth="112dp"
                android:text="Capture"
                android:textColor="#fff" />
            </internetlegion.com.restaurantlunchify.Templates.MaterialRippleLayout>


        </LinearLayout>



    </LinearLayout>

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:progressDrawable="@drawable/circular_progress_bar"
        android:layout_below="@+id/button"
        android:layout_alignRight="@+id/textView"
        android:layout_alignEnd="@+id/textView"
        android:layout_alignLeft="@+id/textview"
        android:layout_alignStart="@+id/textview"
        android:layout_alignParentBottom="true" />


    <ListView
        android:id="@+id/restaurant_list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:dividerHeight="1dp" />

</android.support.v4.widget.DrawerLayout>

图片有目的地约为6mb,我上传看到酒吧,但它是不可见的。你能帮忙的话,我会很高兴。非常感谢。 : - )

1 个答案:

答案 0 :(得分:3)

在此uploadImage(boolean profilePhoto)方法spinner.setVisibility(View.VISIBLE);spinner.setVisibility(View.GONE);将立即被调用,因此您无法看到进度条。

理想情况下,您应该使用AsyncTask这样的

class UploadImageAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        spinner.setVisibility(View.VISIBLE);
    }

    @Override
    protected String doInBackground(Void... params) {
        // Upload image here
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        spinner.setVisibility(View.GONE);
    }
}