Downloading files with progressbar horizontal using parse - android

I have 37 files to download and I wanted to show my progress bar complete in 100%. how can I set that my last file will equal to 100%.. sorry for my english. thanks in advance..
querySound.findInBackground(new FindCallback<ParseObject>() {
#SuppressWarnings("unused")
#Override
public void done(List<ParseObject> objects, ParseException e) {
Log.d("OBJECTS MP3", ""+objects.toString());
if (objects == null) {
Toast.makeText(ParseStarterProjectActivity.this,"Something is Wrong!", Toast.LENGTH_LONG).show();
} else {
final int adder = 100 / objects.size();
for(ParseObject po : objects){
ParseFile sounds = (ParseFile) po.get("remindersSpanish");
final String filenames = sounds.getName().substring(37, sounds.getName().length());
Log.d("OBJECTS NAME", filenames);
sounds.getDataInBackground(new GetDataCallback() {
#Override
public void done(byte[] data,ParseException e) {
Log.d("OBJECTS DATA", ""+data);
if (e == null) {
saveMp3(filenames, data);
counter = counter + adder;
} else {
e.printStackTrace();
}
prgBar.setProgress(counter);
counterPrg.setText(counter+"/"+prgBar.getMax());
}
});

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.

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

Parse query does not work

The problem just like the title, I tried to count the user (I defined) who have the same name, I used countinbackground or findinbackground, no matter what I did, the count is always 0. Does anyone can help me, I don't know why. Thanks a lot. Below is my code:
public boolean checknameunique(String n) {
boolean notexist = true;
ParseQuery<Userdata> query = ParseQuery.getQuery(Userdata.class);
query.whereEqualTo("name", n);
/*query.countInBackground(new CountCallback() {
public void done(int count, ParseException e) {
if (e == null) {
name_count = count;
}
}
});*/
query.findInBackground(new FindCallback<Userdata>() {
#Override
public void done(List<Userdata> objects, ParseException e) {
if(e==null) {
int size = objects.size();
name_count = size;
}
}
});
if(name_count>0) {
notexist = false;
Toast.makeText(this,
"Username already exist",
Toast.LENGTH_LONG).show();
}
return notexist;
}
I guess you are not aware that findInBackground method is asynchronous, that is why you have that callback inside. You probably want to learn a bit more about threads and async tasks in Android and Parse.
Here is the correct code you would want to have:
public void checknameunique(String n) {
ParseQuery<Userdata> query = ParseQuery.getQuery(Userdata.class);
query.whereEqualTo("name", n);
query.findInBackground(new FindCallback<Userdata>() {
#Override
public void done(List<Userdata> res, ParseException e) {
if(e == null) {
int size = res.size();
if(res.size() > 0) {
showMessage("Username already exist");
} else {
proceed();
}
} else {
showMessage(e.getMessage());
}
}
});
}
private void showMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
private void proceed() {
//TODO:
}

Could not parse boolean values in parse

I am trying to parse a boolean value after using it with parseObject, but I cannot parse it?
Here is my code:
ParseQuery<ParseObject> query = ParseQuery.getQuery("SmsTable");
query.whereEqualTo("deviceId", android_id);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, com.parse.ParseException e) {
if (e == null) {
for (ParseObject smsObject : objects) {
if (smsObject!=null) {
Date time = (Date) smsObject.get("date");
myMsg = (String) smsObject.get("message");
usrNum = (String) smsObject.get("phoneNumber");
happend = (boolean) smsObject.get("happend");
result = time;
}
if (result != null) {
if (System.currentTimeMillis() >= result.getTime() && happend == false) {
// count++;
if (usrNum != null && myMsg != null) {
Log.d("message", myMsg);
Log.d("time", String.valueOf(result));
sendMsg2(myMsg, usrNum);
smsObject.put("happend", true);
}
}
}
}
}
}
});
You should call default methods of ParseObject to retrieve data instead of parsing :
ParseQuery<ParseObject> query = ParseQuery.getQuery("SmsTable");
query.whereEqualTo("deviceId", android_id);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, com.parse.ParseException e) {
if (e == null) {
for (ParseObject smsObject : objects) {
if (smsObject!=null) {
Date time = smsObject.getDate("date");
myMsg = smsObject.getString("message");
usrNum = smsObject.getString("phoneNumber");
happend = smsObject.getBoolean("happend");
result = time;
}
if (result != null) {
if (System.currentTimeMillis() >= result.getTime() && happend == false) {
// count++;
if (usrNum != null && myMsg != null) {
Log.d("message", myMsg);
Log.d("time", String.valueOf(result));
sendMsg2(myMsg, usrNum);
smsObject.put("happend", true);
}
}
}
}
}
}
});
You can simply use For loop for getting single object of Parse object and can use getBoolean() method for getting boolean value.
public void done(List<ParseObject> objects, com.parse.ParseException e) {
if (e == null) {
for (int i = 0; i < objects.size(); i++) {
happend = objects.get(i).getBoolean("happend");
}
}
}

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

Categories

Resources