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.
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 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();
I need to pull all the image from parse.
this is the error in the logCat:
Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
The error is here:
String ObjectId = ParseObjects.get(position).getObjectId();
This is my code:
public void PullImage(final ImageView image,int position){
progressDialog = ProgressDialog.show(mContext, "", "Downloading Image...", true);
String ObjectId = ParseObjects.get(position).getObjectId();
query.getInBackground(ObjectId, new GetCallback < ParseObject > () {
public void done (ParseObject object, ParseException e){
if (object != null) {
ParseFile file = (ParseFile) object.get("ImageFile");
file.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
if (e == null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
// surface.addImage(bitmap);
// float Xplace = targetImageforpull.getX();
// float Yplace = targetImageforpull.getY();
image.setImageBitmap(bitmap);
//use this bitmap as you want
progressDialog.dismiss();
} else {
// something went wrong
}
}
});
} else {
Toast.makeText(mContext, "Exception", Toast.LENGTH_SHORT).show();
}
}
});
}
I know that my problem is here:
public void SetParse(){
query = new ParseQuery<ParseObject>("User");
// query.whereEqualTo("Column", bitmap);
size = 0;
try {
size = query.count();
} catch (ParseException e) {
e.printStackTrace();
}
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
ParseObjects = objects;
// The query was successful.
} else {
// Something went wrong.
}
}
});
}
all code:
public class CustomGrid extends BaseAdapter {
private Context mContext;
private Bitmap btimaprecieve;
List<ParseObject> ParseObjects;
ParseQuery<ParseObject> query;
ProgressDialog progressDialog;
int size;
public CustomGrid(Context c) {
mContext = c;
SetParse();
}
//---returns the number of images---
public int getCount() {
return size;
}
//---returns the ID of an item---
public Object getItem(int position) {
return imageId[position];
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(120, 120));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(25, 25, 25, 25);
} else {
imageView = (ImageView) convertView;
}
PullImage(imageView,position);
// imageView.setImageResource(imageId[position]);
return imageView;
}
public void SetParse(){
query = new ParseQuery<ParseObject>("User");
// query.whereEqualTo("Column", bitmap);
size = 0;
try {
size = query.count();
} catch (ParseException e) {
e.printStackTrace();
}
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
// ParseObjects = objects;
ParseObjects = new ArrayList<ParseObject>(objects);
if(ParseObjects == null){
Log.d("ParseObjects is null ", "ParseObjects is null");
}
if(objects == null){
Log.d("objects is null ", "objects is null");
}
// The query was successful.
} else {
// Something went wrong.
}
}
});
}
public void PullImage(final ImageView image,int position){
progressDialog = ProgressDialog.show(mContext, "", "Downloading Image...", true);
String ObjectId = ParseObjects.get(position).getObjectId();
query.getInBackground(ObjectId, new GetCallback < ParseObject > () {
public void done (ParseObject object, ParseException e){
if (object != null) {
ParseFile file = (ParseFile) object.get("ImageFile");
file.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
if (e == null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
// surface.addImage(bitmap);
// float Xplace = targetImageforpull.getX();
// float Yplace = targetImageforpull.getY();
image.setImageBitmap(bitmap);
//use this bitmap as you want
progressDialog.dismiss();
} else {
// something went wrong
}
}
});
} else {
Toast.makeText(mContext, "Exception", Toast.LENGTH_SHORT).show();
}
}
});
}
private Integer [] imageId = {
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
R.drawable.like_button,
};
public Bitmap add(Bitmap bitmap_recieve) {
return btimaprecieve;
}
}
i'm use glide(or any other lib(volley, picasa) for loading ing by URL)
userQuery.getFirstInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, ParseException e) {
if((ParseFile) parseObject.get("photo")!= null){
Glide.with(context).load(((ParseFile) parseObject.get("photo")).getUrl()).asBitmap().dontAnimate().centerCrop().transform(new CircleTransform(context))
.diskCacheStrategy(DiskCacheStrategy.SOURCE).into(hd.logo);
}
}
});
If you need bitmap u can fat it after loading from your img view.
be carefully, if your callback returns ParseArray, u must get object with your picture by position like this ParseObjects.get(position).getObjectId(); But if you have ParseObject you must write ParseObjects.getObjectId();(without get(position)). Check your callback.
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.
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);
}
}