我在Parse.com上有一个RecyclerView
来显示一个类,但图片没有加载。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recipes = new ArrayList<Recipe>();
connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
mInternet = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
//Parse.enableLocalDatastore(getApplicationContext());
// Enable Local Datastore.
if(mWifi.isConnected() && count == 0) {
Log.d("Invoke order","first if");
//Parse.initialize(getApplicationContext(), "HX6n9WMdhKg5BPhC7d22IKHV34jyTst5OHQxtxUD", "U9DotgCbByVP4eZ9oFae61w3Hnno0xFUgefYJOq4");
ParseQuery<ParseObject> query = ParseQuery.getQuery("Collection");
progressDialog = ProgressDialog.show(ListActivity.this, "",
"Update", true);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> recipesList, ParseException e) {
if (e == null) {
ParseObject.pinAllInBackground(recipesList);
Log.d("score", "Retrieved " + recipesList.size() + " recipes");
for ( ParseObject obj : recipesList) {
final ParseObject object = obj;
ParseFile image = (ParseFile) obj.get("File");
image.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
Log.d("picture",Integer.toString(data.length));
if (e == null) {
bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
ImageView image4 = (ImageView) findViewById(R.id.alarmPic);
// Set the Bitmap into the
// ImageView
image4.setImageBitmap(bmp);
initializeData(object, bmp);
Log.d("mytag", "prblem2");
} else {
Log.d("mytag", "coldnt load picture");
}
}
});
initializeData(obj, bmp);
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
initializeAdapter();
progressDialog.dismiss();
}
});
count++;
}if(!mWifi.isConnected() && count != 0) {//error on image
Log.d("Invoke order","second if");
ParseQuery<ParseObject> query = ParseQuery.getQuery("Collection");
query.fromLocalDatastore();
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> list, ParseException e) {
if(e == null){
Log.d("score", "Retrieved " + list.size() + " recipes");
for(ParseObject obj : list){
final ParseObject object = obj;
ParseFile image = (ParseFile)obj.get("File");
image.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
Log.d("picture",Integer.toString(data.length));
if (e == null) {
bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
ImageView image22 = (ImageView) findViewById(R.id.alarmPic);
// Set the Bitmap into the
// ImageView
image22.setImageBitmap(bmp);
initializeData(object, bmp);
Log.d("mytag", "prblem1");
} else {
Log.d("mytag", "coldnt load picture");
}
}
});
initializeData(obj, bmp);
}
}else {
Log.d("score", "Error: " + e.getMessage());
}
initializeAdapter();
}
});
}
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
manager = new LinearLayoutManager(getApplicationContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(manager);
//added to avoid no adapter set exception
//recipes = new ArrayList<>();
RVAdapter adapter = new RVAdapter(recipes);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
private void initializeAdapter(){
RVAdapter adapter = new RVAdapter(recipes);
recyclerView.setAdapter(adapter);
}
private void initializeData(ParseObject obj,Bitmap bmp){
recipes.add(new Recipe(obj.getString("Description"), obj.getString("shortDescription"), obj.getString("Name"), bmp));
Log.d("score", Integer.toString(recipes.size()));
}
}
答案 0 :(得分:1)
您可以使用AsynkTask
class getBitmapFromURL extends AsyncTask<String, Void, Bitmap> {
ParseFile img;
public getBitmapFromURL(ParseFile img) {
this.img = img;
}
protected Bitmap doInBackground(String... urls) {
try {
URL url = new URL(img.getUrl());
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (IOException e) {
}
return null;
}
protected void onPostExecute(final Bitmap bitmap) {
if (bitmap != null) {
image22.setImageBitmap(bitmap);
}
}
}
并像这样使用
ParseFile image = (ParseFile)obj.get("File");
new getBitmapFromURL(image).execute("");
在RecyclerView.Adapter的http://developer.android.com/training/material/lists-cards.html
的onBindViewHolder方法中执行此操作@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
viewHolder.image22.setImageBitmap(bitmap);
}
答案 1 :(得分:0)
只需获取文件网址并将其放入毕加索。
String url = parseFile.getUrl();