Retrieving image files from parse - android - 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);
}
}

Related

Io Failure when try to get data of parsefile

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

Parse.com RecyclerView dont show Image

I have a RecyclerView with Parse.com to show a Class and the image is not loading.
#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()));
}
}
You could use an 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);
}
}
}
and use it like this
ParseFile image = (ParseFile)obj.get("File");
new getBitmapFromURL(image).execute("");
Do this inside the onBindViewHolder method of the RecyclerView.Adapter http://developer.android.com/training/material/lists-cards.html
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
viewHolder.image22.setImageBitmap(bitmap);
}
Just get the files url and put it into Picasso.
String url = parseFile.getUrl();

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.

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