Parse retrieved image in a ListView Android - java

I can't figure out a solution to retrieve images from a Parse Table and display it in a Imageview in my Listview.
Here's what i have so far :
if (mSchedule.getCount() == 0) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("sent_report");
query.selectKeys(Arrays.asList("Logo"));
query.selectKeys(Arrays.asList("Couleur"));
query.selectKeys(Arrays.asList("Date"));
query.selectKeys(Arrays.asList("Rog_pic"));
query.findInBackground(new FindCallback<ParseObject>() {
int i = 0;
Bitmap bmp;
public void done(List<ParseObject> names, ParseException e) {
if (e == null) {
for (ParseObject post : names) {
postTexts.add(post.getString("Logo"));
postTexts.add(post.getString("Couleur"));
postTexts.add(post.getString("Date"));
ParseFile image = (ParseFile) post.get("Rog_pic");
image.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
if (e == null) {
bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
} else {
Log.d("test", "There was a problem downloading the data.");
}
}
});
map = new HashMap<String, Object>();
map.put("nom", postTexts.get(i));
i++;
map.put("titre", postTexts.get(i));
i++;
map.put("description", postTexts.get(i));
i++;
map.put("img", bmp);
listItem.add(map);
mSchedule.notifyDataSetChanged();
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
}
mSchedule is the SimpleAdapter. I tried a lot of things to make it work. I checked, and the ParseFile is not null, so why aren't the images displayed in the Imageview ?
Thanks in advance.

Please post the code for your adapter. Thats where the bug is. Are you sure you're performing a setImageBitmap(img) on your image view within the getView on your adapter? We need to see more adapter code to figure this out.

Related

Parse not deleting objects Android

I was trying to delete objects from Parse Class where requesterUsername is equals to current Username. I have more than 7 rows of data in Parse database but when I execute the method below objects.size() returns 0 which was not my expectation and it does not delete any rows from the database.
I am clueless here. Any help will be appreciated. Thanks in advance.
public void requestGride(View view){
if(requestActive == false) {
Log.i("MyApp", "Gride requested");
ParseObject request = new ParseObject("Requests");
request.put("requesterUsername", ParseUser.getCurrentUser().getUsername());
ParseACL parseACL = new ParseACL();
parseACL.setPublicWriteAccess(true);
request.setACL(parseACL);
request.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
infoTextView.setText("Finding Gride..");
grideX.setText("Cancel GrideX");
requestActive = true;
}
}
});
}else{
infoTextView.setText("GrideX Cancelled");
grideX.setText("GrideX");
requestActive = false;
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Requests");
query.whereEqualTo("requesterUsername",ParseUser.getCurrentUser().getUsername());
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if(e == null){
if(objects.size() > 0){
for (ParseObject adds : objects){
adds.deleteInBackground();
}
}
}
}
});
}
}

Converting activity into fragment is stops the RecyclerView onclick listener from working

I had an activity which was responsible for displaying a RecyclerView List. On clicking an item on the list, I was redirected to another activity along with the data from the recyclerView adapter from the same position.
Now, I am tring to implement a ViewPager tabview, and for that, I have to convert the activity to a fragment. After conversion, the new fragment can display the RecyclerView fine, but the onclick stopped working.
The original activity:
Click to see code
The fragment created from the activity:
Click to see code
strong textThe onclick listener of RecyclerView:
recyclerView.addOnItemTouchListener(
new com.studystory.utilities.RecyclerItemClickListener(getActivity(), new com.studystory.utilities.RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Log.e("position", ""+position);
try {
final Student s = (Student) mAdapter.getObjectAt(position);
final Intent i = new Intent(getActivity().getApplicationContext(), ViewStory.class);
final int pos = position;
if (fromButton.equalsIgnoreCase("browseStoriesButton")) {
i.putExtra("Button", "browseStoriesButton");
} else {
i.putExtra("Button", "notbrowseStoriesButton");
}
String dateOfBirthStr = "";
try {
dateOfBirthStr = TimeSplitterController.generateAge(s.getDateOfBirth().toString());
i.putExtra("dateOfBirthStr", dateOfBirthStr);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
final String dateOfBirthString = dateOfBirthStr;
if (s.getByteArray() == null) {
/* try {
//We assume they have no idea associated.
Bitmap tempBitmap = null;
tempBitmap = ImageController.resizeToHighResolutionCircle(tempBitmap, getApplicationContext());
String bitmapStr = ImageController.bitmapToStringOld(tempBitmap, getApplicationContext());
i.putExtra("bitmapStr", bitmapStr);
tempBitmap.recycle();
tempBitmap = null;
} catch (Exception e) {
Log.e("Exception",e.toString());
}*/
} else {
//They have an image.
Bitmap tempBitmap = ImageController.BitmapCompress(s.getByteArray());
if (tempBitmap != null) {
tempBitmap = ImageController.resizeToHighResolutionCircle(tempBitmap, getActivity().getApplicationContext());
} else {
tempBitmap = ImageController.resizeToCircle(tempBitmap, getActivity().getApplicationContext());
}
String bitmapStr = ImageController.bitmapToStringOld(tempBitmap, getActivity().getApplicationContext());
i.putExtra("bitmapStr", bitmapStr);
tempBitmap.recycle();
tempBitmap = null;
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
i.putExtra("studentObject", s);
startActivity(i);
}
}, 50);
Log.e("Clicked", "" + position);
}
catch (Exception e){
Log.e("List issue", e.toString());
}
}
})
);
The logcat output in fragment:
List issue: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference
Where am I going wrong? Why the fragment cannot find the objects from the adapter while the activity can?
Can you please add code like below ?
if(fromButton!=null){
if (fromButton.equalsIgnoreCase("browseStoriesButton")) {
i.putExtra("Button", "browseStoriesButton");
} else {
i.putExtra("Button", "notbrowseStoriesButton");
}
}else{
i.putExtra("Button", "notbrowseStoriesButton");
}
Hope this will help you.

Download file from Parse throws NullPointerException

In my application i can successfully upload file to parse.com. But when I tried to download it, it is giving null pointer exception.Here is my code to download file.
ParseObject downloadData = new ParseObject("DownloadData");
ParseFile downloadFile = (ParseFile) downloadData.get("File");
downloadFile.getDataInBackground(new GetDataCallback() {
#Override
public void done(byte[] bytes, ParseException e) {
if (e == null) {
String x= new String(bytes);
new AlertDialog.Builder(MainActivity2.this)
.setTitle("Downloaded File")
.setMessage(x)
.setPositiveButton("Ok", null)
.show();
} else {
new AlertDialog.Builder(MainActivity2.this)
.setTitle("Download File")
.setMessage("An Error Occurred")
.setPositiveButton("Ok", null)
.show();
}
}
});
the official documentation is confusing. Can anyone tell me a way to fix this.
You can not call get() on ParseObject created like this.
First you need to call parseQuery on your Parse Class and get this ParseObject from this query result.Now call get() on this ParseObject.
ParseQuery<ParseObject> query = ParseQuery.getQuery("DownloadData");
query.getInBackground("parse_object_id", new GetCallback<ParseObject>() {
public void done(ParseObject downloadData, ParseException e) {
if (e == null) {
// This object will contain your file
ParseFile downloadFile = (ParseFile) downloadData.get("File");
downloadFile.getDataInBackground(new GetDataCallback() {
#Override
public void done(byte[] bytes, ParseException e) {
if (e == null) {
String x= new String(bytes);
new AlertDialog.Builder(MainActivity2.this)
.setTitle("Downloaded File")
.setMessage(x)
.setPositiveButton("Ok", null)
.show();
} else {
new AlertDialog.Builder(MainActivity2.this)
.setTitle("Download File")
.setMessage("An Error Occurred")
.setPositiveButton("Ok", null)
.show();
}
}
});
} else {
// something went wrong
}
});

Download 5 images from urls

Im trying to download 5 images from the response of a JSON, i have managed to get the URLs of the images and am able to download the image if i hard code one of the image locations into the code.
How would i do it so that i can download all 5 images.
Below is the request code:
public void getImage(String url, final ImageView imageView) {
ImageRequest requestImage = new ImageRequest(url, new Response.Listener<Bitmap>() {
#Override
public void onResponse(Bitmap response) {
System.out.println("Image Url is: " + response);
imageView.setImageBitmap(response);
System.out.println();
}
}, 0, 0, null, null);
queue.add(requestImage);
}
Below is the code that passes the image Url from the response and sets the image in the xml based on the ID
FYI: bp_promo1 is the hard coded image added into the request
try {
System.out.println("Size of PromoItemsArray is: " + home.promoItemsArray.size());
for (int i = 0; i < home.promoItemsArray.size(); i++) {
String imageUrl = home.promoItemsArray.get(i).imageUrl;
request.getImage(imageUrl, bp_promo1);
}
} catch (Exception e) {
System.out.println("Error is: " + e + " - Exception is it: " + e.getStackTrace()[2].getLineNumber());
}
}
My idea was to add all five images to an array then pass each item in the array to the network call?
Thanks
The way I managed to fix the issue was below?
public void getImage(String url, final Object object) {
ImageLoader.ImageCache imageCache = new BitmapLruCache();
ImageLoader imageLoader = new ImageLoader(queue, imageCache);
imageLoader.get(url, new ImageLoader.ImageListener() {
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
Bitmap responseBitmap = response.getBitmap();
ImageView imageView;
ImageButton imageButton;
HorizontalScrollView horizontalScrollView;
if (object instanceof ImageView) {
imageView = (ImageView) object;
imageView.setImageBitmap(responseBitmap);
} else if (object instanceof ImageButton) {
imageButton = (ImageButton) object;
imageButton.setImageBitmap(responseBitmap);
} else if (object instanceof HorizontalScrollView) {
horizontalScrollView = (HorizontalScrollView) object;
horizontalScrollView.setBackground(new BitmapDrawable(getResources(), responseBitmap));
}
}
I just passed through a Object and manipulated the code based on the source of the Object type

Storing HashMap<String, Bitmap> in ArrayList - can't decode Bitmaps upon displaying

I have an ArrayList which stores a HashMap of Bitmaps, but when I try to display them with ListView adapter it shows this error(and yes I am absolutely sure that Uri is correct and links to an image):
08-01 14:03:53.103: E/BitmapFactory(7225): Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap#656c2d18: open failed: ENOENT (No such file or directory)
08-01 14:03:53.103: I/System.out(7225): resolveUri failed on bad bitmap uri: android.graphics.Bitmap#656c2d18
Here's my code:
ArrayList<HashMap<String, Bitmap>> imgList = new ArrayList<HashMap<String, Bitmap>>();
for(int i = 0; i < jData.length(); i++) {
JSONObject c = jData.getJSONObject(i);
//get the imageURL and set it to string
JSONObject images = c.getJSONObject(TAG_IMAGES);
JSONObject thumbnail = images.getJSONObject(TAG_THUMBNAIL);
String imageURL = thumbnail.getString(TAG_URL);
//Decode Bitmap before putting in HashMap
try {
InputStream in = new java.net.URL(imageURL).openStream();
pic = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
//create new HashMap
HashMap<String, Bitmap> bitmap=new HashMap<String, Bitmap>();
//put HashMap in the ArrayList
bitmap.put(TAG_PIC, pic);
imgList.add(bitmap);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
// and finally display them in ImageView
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
ActivityMain.this, imgList,
R.layout.list_item, new String[] { TAG_PIC },
new int[] { R.id.ivThumb
});
setListAdapter(adapter);
}
I have also tried using for loop to go through all of the lv items and setting imageViews but it using this code it only sets the ImageView of the first item (but loops through all images)
for(childIndex = 0; childIndex < lv.getAdapter().getCount(); childIndex++) {
// lv.setSelection(childIndex);
runOnUiThread(new Runnable() {
#Override
public void run() {
// childView = lv.getChildAt(childIndex);
//lv.setSelection(childIndex);
iv = (ImageView) findViewById(R.id.ivThumb);
iv.setImageBitmap(pic);
}
});
}
So if you could help me do this rather simple but a little confusing thing, it would be great! (I would prefer using hashmap, because it is easier for me to understand, but any way that works will be great!)
EDIT: Added FOR loop and corrected HashMap statement placement.

Categories

Resources