【问题标题】:Loading image from Parse and set in imageView从 Parse 加载图像并在 imageView 中设置
【发布时间】:2017-07-07 02:04:27
【问题描述】:

我已经能够从 Parse 中提取字符串数据并将其设置为 textviews,但我无法获取图像文件并将其设置在 imageview 中,我尝试使用 Byte 方法但没有成功并出现错误数据长度。这是我的代码:

public class mygardenDetail extends Activity {

String name;
//String image;
String kgsays;
String care;
String tips;
String image;
Context context;
ImageLoader imageLoader = new ImageLoader(this);
List<ParseObject> ob;
private List<PlantListitems> plantlist = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from singleitemview.xml
    setContentView(R.layout.mygarden_detail);
    //new mygardenDetail.RemoteDataTask().execute();
    imageLoader = new ImageLoader(context);

    Intent i = getIntent();
    // Get the result of rank
    name = i.getStringExtra("name");
    //Get the result of country
    // kgsays = i.getStringExtra("kgsays");
    // Get the result of country
    //care = i.getStringExtra("care");
    // Get the result of population
    //tips = i.getStringExtra("tips");
    // Get the result of flag
    // image = i.getStringExtra("image");


    // Locate the TextViews in singleitemview.xml

    TextView txtName = (TextView) findViewById(R.id.name);
    final TextView txtKGsays = (TextView) findViewById(R.id.KGsays);
    final TextView txtCare = (TextView) findViewById(R.id.Care);
    final TextView txtTips = (TextView) findViewById(R.id.Tips);

    // Locate the ImageView in singleitemview.xml
    final ImageView imgflag = (ImageView) findViewById(R.id.image);
    //Set values in containers
    txtName.setText(name);
    imageLoader.DisplayImage(image, imgflag);

    ParseQuery query = new ParseQuery<>("Plants2");
    query.whereEqualTo("plantName", (name));
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> listCountry, ParseException e) {

            for (ParseObject country : listCountry) {


                // Locate images in flag column
                ParseFile image = (ParseFile) country.get("image");


                //PlantListitems map = new PlantListitems();
                txtKGsays.setText((String) country.get("KGsays"));
                txtCare.setText((String) country.get("Care"));
                txtTips.setText((String) country.get("tips"));
                Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
                imgflag.setImageBitmap(bmp);

                //plantlist.add(map);
                //imageLoader.DisplayImage(image, imgflag);
            }
        }


    });
}
}

用于通过意图在列表视图中显示的代码:

  // RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {



    @Override
    protected Void doInBackground(Void... params) {
        // Create the array
        worldpopulationlist = new ArrayList<WorldPopulation>();
        try {
            // Locate the class table named "Country" in Parse.com
            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                    "Plants2");
            // Locate the column named "ranknum" in Parse.com and order list
            // by ascending
            query.orderByAscending("plantName");
            ob = query.find();
            for (ParseObject country : ob) {
                // Locate images in flag column
                ParseFile image = (ParseFile) country.get("image");

                WorldPopulation map = new WorldPopulation();
                map.setName((String) country.get("plantName"));
                map.setType((String) country.get("type"));
                map.setPlot((String) country.get("plotSize"));
                map.setImage(image.getUrl());
                worldpopulationlist.add(map);
            }
        } catch (ParseException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

【问题讨论】:

  • 您可以使用任何库 Glide、Universal 或 picasso 在 imageview 上显示它
  • 请问我该如何实现这个方法?
  • 参考这个答案stackoverflow.com/a/39972276/3946958 ...他们用过glide库
  • 检查我的以下解决方案,如果有问题请告诉我
  • 感谢您的回复,但建议应该容易得多。我正在从 Parse 后端加载数据。

标签: java android parsing imageview pfobject


【解决方案1】:

此源代码似乎可以从文件加载图像:

Bitmap bitmap = BitmapFactory.decodeFile(pathToImage);

Bitmap 和 BitmapFactory 类位于 android.graphics 包中:

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

假设您的图像路径是正确的,您可以像这样将此位图图像添加到 ImageView:

ImageView imageView = (ImageView) getActivity().findViewById(R.id.imageView);
imageView.setImageBitmap(BitmapFactory.decodeFile(pathToPicture));

学分 - http://alvinalexander.com/source-code/android/android-how-load-image-file-and-set-imageview

【讨论】:

    【解决方案2】:

    您可以使用 Glide 库从服务器加载图像,如下所示:-

    Glide 的 Gradles 条目

    // For the glide libraray
    compile 'com.github.bumptech.glide:glide:3.7.0'
    

    就像这是你的 Imageview 一样,你可以使用进度条来显示从服务器加载图像的过程

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
    
            <ProgressBar
                android:id="@+id/YOUR_PROGRESSBAR"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_centerInParent="true"
                />
    
            <ImageView
                android:id="@+id/YOUR_IMAGEVIEW"
                android:layout_width="match_parent"
                android:layout_height="120dp"
                android:textColor="@android:color/black"
                android:layout_centerInParent="true"
                android:visibility="invisible"
                android:src="@drawable/bubble1" />
    
        </RelativeLayout>
    

    在你的Activity/Fragment 中使用这行代码

      YOUR_PROGRESSBAR.setVisibility(View.VISIBLE);
     Glide.with(mContext)
                    .load("your_server_url")
                    .fitCenter()
                    .crossFade()
                    .listener(new RequestListener<String, GlideDrawable>() {
                        @Override
                        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                            if (e instanceof UnknownHostException)
                                YOUR_PROGRESSBAR.setVisibility(View.GONE);
    
                            return false;
                        }
    
                        @Override
                        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                            YOUR_PROGRESSBAR.setVisibility(View.GONE);
                            YOUR_IMAGEVIEW.setVisibility(View.VISIBLE);
                            return false;
                        }
                    }).into(YOUR_IMAGEVIEW);;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2013-06-19
      相关资源
      最近更新 更多