Io Failure when try to get data of parsefile - android

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

Related

Parse Query objects.size always returns 0 Android

public void pictureQuery() {
friendsPic = new ArrayList<>();
for (int i = 0; i < friendsList.length(); i++) {
ParseQuery parseQuery = ParseQuery.getQuery("Data");
parseQuery.whereEqualTo("firstName", "John");
//parseQuery.whereEqualTo("userId",friendsID.get(i));
parseQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if (objects.size() > 0) {
String link = objects.get(0).get("picUrl").toString();
Bitmap bitmap = null;
try {
bitmap = task.execute(link).get();
friendsPic.add(bitmap);
} catch (Exception e1) {
e1.printStackTrace();
}
Log.i("FDA, done", friendsPic.toString());
} else {
Log.i("FD", "0002");
}
} else {
e.printStackTrace();
}
}
});
}
}
Hi guys,
I am trying to retrieve picture URL from parse server (stored as a string), originally I was trying to make a query using the Id, but also tried to hard code it using:
parseQuery.whereEqualTo("firstName", "John");
It always seems to go to the else statement of object.size() > 0 i.e.
Log.i("FD", "0002");
I think i've checked everything including:
size of friendsList ( for the for loop )
spelling, made sure that "Data", "firstName" and "John" are the same on Parse Server
The value of friendsID.get(i)
Tried to use findFirstInBackground instead (same issue)
I have also compared it with my other code for different data ( that works ) :
ParseQuery parseQuery = ParseQuery.getQuery("SportData");
parseQuery.whereEqualTo("id", mApp.getId());
parseQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if (objects.size() > 0) {
saveSportData(objects.get(0), false);
} else if (objects.size() < 1) {
saveSportData(null, true);
}
} else {
e.printStackTrace();
}
}
});
Any idea what am I doing wrong, or what may be causing it ?
Thanks a lot.

Error: Convert Drawable To Bitmap Inside Fragment

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.

Android : load an imageview from a Parsefile

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.

Retrieving image files from parse - android

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);
}
}

storing multiple ParseFile objects in single ParseObject

ANDROID
This is how I store ParseFile List into ParseObject
ParseObject pObject = new ParseObject();
ArrayList<ParseFile> pFileList = new ArrayList<ParseFile>();
for (String thumbPath : thumbList) {
byte[] imgData = convertFileToByteArray(thumbPath);
ParseFile pFile = new ParseFile("mediaFiles",imgData);
pFileList.add(pFile);
}
pObject.addAll("mediaFiles", pFileList);
pObject.saveEventually();
after this call it does not show the inserted row in data browser, although it shows rowcount of 1 in table
This is how i retrieve it and get the first image from the list
List<ParseFile> pFileList = (ArrayList<ParseFile>) pObject.get("mediaFiles");
if (!pFileList.isEmpty()) {
ParseFile pFile = pFileList.get(0);
byte[] bitmapdata = pFile.getData(); // here it throws error
bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);
}
I am able to retrieve all String columns , but for "mediaFiles" column while doing getData() I get thisexception.
com.parse.ParseException: Target host must not be null, or set in parameters.
I observed that in ParseFile, data and url is null.
Can somebody please show me the code on how to store and retrieve multiple ParseFile objects into single ParseObject?
Try uploading the ParseFiles using the save method before associating them with the Parse Object.
Try to do this thing as upload image one after another,
public void UploadImageToParse(String img_path, final String filedName, String Filename) {
//String img_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "P200913_1908.jpg";
final File file = new File(img_path);
try {
FileInputStream fileInputStream = new FileInputStream(file);
Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] image_array = baos.toByteArray();
final ParseFile parsefile = new ParseFile(Filename, image_array);
parsefile.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e != null) {
} else {
frameInfo.put(filedName, parsefile);
frameInfo.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
DebugLog.e("" + e);
} else {
fileUploadStatusUpdater.onFailure();
DebugLog.e("File Upload Fail");
}
}
});
/*ParseUser user = ParseUser.getCurrentUser();
user.put(filedName, parsefile);
user.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
Log.e("", "" + e);
}
});*/
}
}
}, new ProgressCallback() {
#Override
public void done(Integer integer) {
if (integer == 100) {
DebugLog.e("File Upload Completed");
fileUploadStatusUpdater.onSuccess();
}
}
});
} catch (Exception e) {
DebugLog.e("Fis" + e);
fileUploadStatusUpdater.onFailure();
}
Cover the
byte[] bitmapdata = pFile.getData();
line of code under try catch. It worked for me!
final ParseFile parseFile1 = new ParseFile("poll_image1.jpg",scaleImage("poll_image1",imageList.get(contestImage1.getId())));
final ParseFile parseFile2 = new ParseFile("poll_image2.jpg",scaleImage("poll_image2",imageList.get(contestImage2.getId())));
parseFile1.save(); parseFile2.save();
List<ParseFile> listOfFiles = new ArrayList<ParseFile>();
listOfFiles.add(parseFile1);
listOfFiles.add(parseFile2);
ParseObject jobApplication = new ParseObject("Poll");
jobApplication.put("poll_question", contestQuestion.getText().toString());
jobApplication.put("poll_type_id", 1);
ParseUser currentUser = ParseUser.getCurrentUser();
jobApplication.put("user", currentUser);
jobApplication.put("parseFile", listOfFiles);
jobApplication.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException arg0) {
}
});
Above code does multi upload, but ParseObject called only after Two save() method. Because of two save method UI getting Stuck. How to fix it!

Categories

Resources