Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在将图像从设备库加载到imageView时显示progressbar_Android_Android Progressbar - Fatal编程技术网

Android 如何在将图像从设备库加载到imageView时显示progressbar

Android 如何在将图像从设备库加载到imageView时显示progressbar,android,android-progressbar,Android,Android Progressbar,在我的android应用程序中,我提供了一个从设备库上传图像的选项。它需要一些时间来加载,所以我想包括一个progressbar。我使用了spinner.setVisibility(View.VISIBLE)方法,但它不工作。我附加了我的代码片段 protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivi

在我的android应用程序中,我提供了一个从设备库上传图像的选项。它需要一些时间来加载,所以我想包括一个progressbar。我使用了
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" />

请帮助我更正代码。

使用异步任务

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 );
 }
};
公共类MyTask扩展了AsyncTask{
私人ProgressBar ProgressBar;
公共MyTask(ProgressBar ProgressBar){
this.progressBar=progressBar;
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
progressBar.setVisibility(View.VISIBLE);
//做你的工作
返回“OK”;
}
@凌驾
受保护的void onPostExecute(ArrayList结果){
progressBar.setVisibility(View.GONE);
}
};
你确定这不起作用吗? 看起来,用户并没有足够的时间看到UI中的差异。
也许
spinner.setVisibility(View.VISIBLE)
spinner.setVisibility(View.GONE)
内部的操作真的很快吗?

据我所知,activityResult是在UI线程上运行的,也许尝试将所有内容迁移到异步任务?当从gallery加载图像到imageview时,两者之间存在延迟,当我运行代码应用程序时,在从gallery选择图像后停止
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);