I'm trying to find a ParseObject by " objectId ", then retrieve the image " ImageFile " and then Load it to the imageview, it doesn't work and i'm getting the USER String, can you help me out with this, it works when i use another query like : query.find()
ParseImageView mealImage = (ParseImageView) findViewById(R.id.icon);
ParseQuery<ParseObject> query1 = ParseQuery.getQuery("Annonces");
query1.getInBackground("ux3Af0cwEx", new GetCallback<ParseObject>() {
public void done(ParseObject Annonces, ParseException e) {
photoFile = (ParseFile) Annonces.get("ImageFile");
text1.setText((CharSequence) Annonces.get("USER"));
}
});
mealImage.setParseFile(photoFile);
mealImage.loadInBackground(new GetDataCallback() {
#Override
public void done(byte[] data, ParseException e) {
}
});
}
The code for displaying image in imageview:
ParseFile image = (ParseFile) userData.getParseFile("user_image");
then call following function.
loadImages( photoFile, mealImage);
private void loadImages(ParseFile thumbnail, final ImageView img) {
if (thumbnail != null) {
thumbnail.getDataInBackground(new GetDataCallback() {
#Override
public void done(byte[] data, ParseException e) {
if (e == null) {
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
img.setImageBitmap(bmp);
} else {
}
}
});
} else {
img.setImageResource(R.drawable.menu);
}
}// load image
If you are using Picasso or Glide for image loading and don't want to change the image loading logic, you can extract image url from ParseFile and load it in background.
Like:
ParseFile thumbnail = parseObject.getParseFile("image");
if(thumbnail != null) {
String imageUrl = thumbnail.getUrl();
Picasso.with(mContext).load(imageUrl).into(imageView);
}
No need to load thumbnail ParseFile data separately.
Related
Error throw :I/O failure
My code
Parsefile thumbnail=products.get(0).getParseFile("image");
if (thumbnail != null) {
thumbnail.getDataInBackground(new GetDataCallback() {
#Override
public void done(byte[] data, ParseException e) {
if (e == null) {
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
picsbitmap.put(itemname,bmp);
Log.d("nwck", String.valueOf(picsbitmap.size()));
} else {
Log.d("enaproblem",e.getLocalizedMessage());
}
}
});
} else {
}
My thumbnail is not
null but I get an exception on the done method , I try to retrieve png file and picsbitmap is hashmap<String,Bitmap> type and is there any way to get image from Aws server
I need to update the list with images retrieved in the background using Parse service. Using below code, I could retrieve images and display but interaction is quite slow. Is there any better way to update the ListView dynamically without impacting user interaction speed?
ParseQuery<ParseObject> userFeedQuery = ParseQuery.getQuery("Offers");
userFeedQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null){
if (objects.size() > 0){
for (ParseObject object:objects){
final String offerName = object.getString("offerName");
final String offerDetail = object.getString("offerDetails");
final Bitmap[] offerImage = new Bitmap[1];
ParseFile file = (ParseFile) object.getParseFile("offerImage");
file.getDataInBackground(new GetDataCallback() {
#Override
public void done(byte[] data, ParseException e) {
if (e == null){
offerImage[0] = BitmapFactory.decodeByteArray(data,0,data.length);
offerModelList.add(new OfferModel(offerName,offerDetail, offerImage[0]));
adapter.notifyDataSetChanged();
}
}
});
}
}
adapter = new OffersAdapter(getApplicationContext(),R.layout.offers_table,offerModelList);
offersListView.setAdapter(adapter);
}
}
});
Yes, you can replace ParseImageView with Picaso or Glide, but i prefere Picaso.
Replace
ParseFile file = (ParseFile) object.getParseFile("offerImage");
With this
String OfferImageUrl = object.getParseFile("offerImage");
if (!TextUtils.isEmpty(OfferImageUrl)) {
Picasso.with(this) // use getContext or contex for fragments or adapter
.load(OfferImageUrl)
.error(android.R.drawable.error) // your own error image
.into(mOfferImage); // mOfferImage = (ImageView) findViewById(R.id.offer_image);
}
Hope this help. Let me know for any assistance about this question.
I am trying to download the image from parse.com and display in a image view in an Activity extending AppCompatActivity.
I got this code from one of many searches:
ParseImageView mImage = (ParseImageView) findViewById(R.id.image);
ParseObject object = new ParseObject("Appetizers"); // class name
ParseFile postImage = object.getParseFile("imageFiles"); // column name
String imageUrl = postImage.getUrl() ;//live url
Uri imageUri = Uri.parse(imageUrl);
Picasso.with(getBaseContext()).load(imageUri.toString()).into(mImage);
File format: jpg
Here's my solution. On my activity, extending AppCompatActivity, this is the code to download an image inside ImageView:
ImageLoader.ImageCache imageCache = new BitmapLruCache();
ImageLoader imageLoader = new ImageLoader(Volley.newRequestQueue(getApplicationContext()), imageCache);
NetworkImageView headshot = (NetworkImageView) findViewById(R.id.speaker_headshot);
headshot.setImageResource(R.drawable.loading);
headshot.setImageUrl("http://www.anydomain.com/anyimage.jpg", imageLoader);
As you see you will need an addicional class in your project, a file named BitmapLruCache.java. Here is the entire content of it:
public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache {
public BitmapLruCache() { this(getDefaultLruCacheSize()); }
public BitmapLruCache(int sizeInKiloBytes) { super(sizeInKiloBytes); }
#Override
protected int sizeOf(String key, Bitmap value) { return value.getRowBytes() * value.getHeight() / 1024; }
#Override
public Bitmap getBitmap(String url) { return get(url); }
#Override
public void putBitmap(String url, Bitmap bitmap) { put(url, bitmap); }
public static int getDefaultLruCacheSize() {
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
return cacheSize;
}
}
Finally, for this to work you have to setup a special ImageView inside your layout XML:
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/speaker_headshot"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"/>
EDIT: For all of this to work you have to import:
1) the Volley library. Do this on your Android Studio: menu File/Project Structure/Modules-app/Tab Dependencies. There you add (+ sign) a Library Dependency and find or search for com.android.volley:volley:1.0.0. That's it. OR simple include this in your build.gradle (module app) file:
dependencies {
compile 'com.android.volley:volley:1.0.0'
}
If you already have a "dependencies" section then just include the line inside it.
AND
2) Put a copy of disklrucache-2.0.2.jar into your "libs" folder ( get the .jar from here https://search.maven.org/remote_content?g=com.jakewharton&a=disklrucache&v=LATEST ) OR insert another "compile" directive inside your "dependencies":
compile 'com.jakewharton:disklrucache:2.0.2'
try on this way.
ParseObject object = new ParseObject("Appetizers"); // class name
ParseFile postImage = object.getParseFile("imageFiles"); // column name
ParseQuery<ParseObject> getimage = new ParseQuery<ParseObject>("Appetizers"); // class
getimage.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
// TODO Auto-generated method stub
if (e == null) {
// success
for (ParseObject parseObject : objects) {
ParseFile fileObject = (ParseFile) parseObject.get("imageFiles");
Log.d("test", "get your image ... " + fileObject.getUrl());
Picasso.with(getBaseContext()).load(fileObject.getUrl()).placeholder(R.drawable.ic_launcher)
.into(mImage);
}
} else {
// fail
Log.d("test", "error Message... " + e.getMessage());
}
}
});
your have not set picasso place holder for clear catch memory form picasso lib.
I figured it out by debugging and setting logs everywhere ...
In the sending class:
ParseObject po = mAppetizers.get(position); // get position
String ID = po.getObjectId().toString();
Intent intent = new Intent(Appetizer.this, AppetizerRecipe.class);
intent.putExtra("ID", ID);
startActivity(intent);
In the receiver's class:
final ParseImageView mImage = (ParseImageView) findViewById(R.id.image);
String ID = getIntent().getStringExtra("ID");
ParseQuery<ParseObject> getimage = new ParseQuery<>("Appetizers");
getimage.addAscendingOrder("appetizer");
getimage.whereEqualTo("ID", ID);
Log.d("AppetizerRecipe2", "object: " + ID);
getimage.getInBackground(ID, new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
if (e == null) {
Log.v("what is e?", "e = " + e);
// success
final ParseFile fileObject = (ParseFile)object.get("imageFiles");
fileObject.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
if (e == null) {
Log.d("test", "We've got data in data.");
// use data for something
Log.d("test", "Get your image..." + fileObject.getUrl());
Picasso.with(getBaseContext()).load(fileObject.getUrl()).placeholder
(R.drawable.ic_launcher).into(mImage);
} else {
Log.d("test", "There was a problem downloading the data.");
}
}
});
} else {
// fail
Log.d("test", "Error Message..." + e.getMessage());
}
}
});
Hi I am populating a custom listview from parse user table. In that if the row doesn't containing a image I want to show one local image. For that I need to convert the drawable image into bitmap inside a fragment. I tried couple of methods. But none work and I don't know what is the error.
And my code is...
postImage = po.getParseFile("pic");
if (postImage != null && postImage.getUrl() != null && postImage.getUrl().length() > 0) {
postImage.getDataInBackground(new GetDataCallback() {
#Override
public void done(byte[] bytes, ParseException e) {
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
people.setPic(bmp);
}
});
} else {
Bitmap icon = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.sanjay);
Log.d("ImageCoversion", BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.sanjay).toString());
people.setPic(icon);
}
I am getting error in getActivity().getResources() inside else block.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.support.v4.app.FragmentActivity.getResources()' on a null object reference
And my full doInBackground code is
protected List<People> doInBackground(List<People>... params) {
try {
final ParseQuery<ParseUser> query = ParseUser.getQuery();
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
ParseFile image = null;
if (e == null) {
peopleList.clear();
obj = objects;
for (ParseObject po : objects) {
//image = (ParseFile) po.get("pic");
ParseFile postImage;
final People people = new People();
people.setName(po.getString("username"));
people.setLastMessage(po.getString("email"));
people.setObjectId(po.getObjectId());
//people.setProfilePic(image.getUrl());
postImage = po.getParseFile("pic");
if (postImage != null && postImage.getUrl() != null && postImage.getUrl().length() > 0) {
postImage.getDataInBackground(new GetDataCallback() {
#Override
public void done(byte[] bytes, ParseException e) {
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
people.setPic(bmp);
}
});
} else {
Bitmap icon = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.sanjay);
Log.d("ImageCoversion", icon.toString());
people.setPic(icon);
}
peopleList.add(people);
}
} else {
Log.d("*****Error", e.getMessage());
}
}
});
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return peopleList;
}
Try declaring Activity a = getActivity(); at the beginning of your code
In this else instead of :
Bitmap icon = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.sanjay);
use :
Bitmap icon = BitmapFactory.decodeResource(a.getResources(), R.drawable.sanjay);
Sometimes getActivity() returns null.
I'm trying to retrieve images that I have upload from my app:
intent = getIntent();
String id = intent.getStringExtra("id");
ParseQuery<ParseObject> query = ParseQuery.getQuery("Items");
query.getInBackground(id, new GetCallback<ParseObject>()
{
#Override
public void done(ParseObject object, ParseException e)
{
if (e == null)
{
setTitle(object.getString("name"));
textPlatform.setText(object.getString("platform"));
textPrice.setText(String.valueOf(object.getDouble("price")));
textDelivery.setText(String.valueOf(object.getDouble("delivery")));
textLocation.setText(object.getString("location"));
textCondition.setText(object.getString("condition"));
textSeller.setText(object.getString("seller"));
ParseFile applicantResume = (ParseFile) object.get("image");
applicantResume.getDataInBackground(new GetDataCallback()
{
public void done(byte[] data, ParseException e)
{
if (e == null)
{
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
imgItem.setImageBitmap(bmp);
}
else
{
e.printStackTrace();
}
}
});
} else
{
e.printStackTrace();
}
}
});
I can successfully retrieve the other items just not the file (which I know exists and is under the column "image").
Thank You in advanced
This is how i am doing it:
I get the file from parse using getParseFile method:
ParseFile postImage = object.getParseFile(ParseConstants.PARSE_KEY_FILE);
String imageUrl = postImage.getUrl() ;//live url
Uri imageUri = Uri.parse(imageUrl);
and then I use Picasso to display the image:
Picasso.with(context).load(imageUri.toString()).into(mPostImage);
by this you can display the image....
ParseFile image = (ParseFile) userData.getParseFile("user_image");
//call the function
displayImage(image, image_expert);
//and here is the function
private void displayImage(ParseFile thumbnail, final ImageView img) {
if (thumbnail != null) {
thumbnail.getDataInBackground(new GetDataCallback() {
#Override
public void done(byte[] data, ParseException e) {
if (e == null) {
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0,
data.length);
if (bmp != null) {
Log.e("parse file ok", " null");
// img.setImageBitmap(Bitmap.createScaledBitmap(bmp,
// (display.getWidth() / 5),
// (display.getWidth() /50), false));
img.setImageBitmap(getRoundedCornerBitmap(bmp, 10));
// img.setPadding(10, 10, 0, 0);
}
} else {
Log.e("paser after downloade", " null");
}
}
});
} else {
Log.e("parse file", " null");
// img.setImageResource(R.drawable.ic_launcher);
img.setPadding(10, 10, 10, 10);
}
}