An Image Downloaded From Parse Stay On Screen Even After You Exit And Reopen App?
Asked Answered
S

3

0

I have an app that downloads an image from Parse.com and displays that image in an Image View

The problem is that whenever I exit the app (with the back button) and return the image is gone.

How can I make the image stay?

(For example: when you update your profile pic on Twitter and leave the app and return your profile pic will still be displayed)

Any help would be greatly appreciated this is very important.

MainActivity:

public class MainActivity extends Activity {
    Button button;
    private ProgressDialog progressDialog;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from main.xml
        setContentView(R.layout.activity_main);
        // Show progress dialog

        // Locate the button in main.xml
        button = (Button) findViewById(R.id.button);

        // Capture button clicks
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {

                progressDialog = ProgressDialog.show(MainActivity.this, "",
                        "Downloading Image...", true);

                // Locate the class table named "ImageUpload" in Parse.com
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                        "ImageUploads");

                // Locate the objectId from the class
                query.getInBackground("h3FvFzrHPr",
                        new GetCallback<ParseObject>() {

                            public void done(ParseObject object,
                                    ParseException e) {
                                // TODO Auto-generated method stub

                                // Locate the column named "ImageName" and set
                                // the string
                                ParseFile fileObject = (ParseFile) object
                                        .get("imageContent");
                                fileObject
                                        .getDataInBackground(new GetDataCallback() {

                                            public void done(byte[] data,
                                                    ParseException e) {
                                                if (e == null) {
                                                    Log.d("test",
                                                            "We've got data in data.");
                                                    // Decode the Byte[] into
                                                    // Bitmap
                                                    Bitmap bmp = BitmapFactory
                                                            .decodeByteArray(
                                                                    data, 0,
                                                                    data.length);

                                                    // Get the ImageView from
                                                    // main.xml
                                                    ImageView image = (ImageView) findViewById(R.id.image);

                                                    // Set the Bitmap into the
                                                    // ImageView
                                                    image.setImageBitmap(bmp);

                                                    // Close progress dialog
                                                    progressDialog.dismiss();

                                                } else {
                                                    Log.d("test",
                                                            "There was a problem downloading the data.");
                                                }
                                            }
                                        });
                            }
                        });
            }

        });
    }
}
Suhail answered 2/1, 2016 at 0:52 Comment(11)
Is the onCreate method running again when you re-open the app? try by debugging and putting a breakpoint somewhere within the onCreate and also the query call backRisorgimento
Okay so I set a log.d message "onCreate runs" and it didn't display when I ran the app for the first time but when I pressed the back button and exited the app then the square button (all running applications button) to to display the app and select it to reopen it, the log.d message "onCreate runs" can be seen in the LogcatSuhail
Just a heads up, I am trying to develop a profile image app so how users on social media can take a pic or upload a pic and save it and when they return they still see the pic, that's the main idea of what I am trying to do. I know how to take/upload the pic to Parse.com but to download it and display it same time is the issue. The pic won't stay on the screen. Is it due to the onCreate running instead of onResume?Suhail
Is the ParseException variable null when you reopen the app? This may tell you what your problem is.Risorgimento
I am Assuming the ParseException variable is null or I would see " Log.d("test", "There was a problem downloading the data."); "Suhail
Didn't encounter any errors when downloading the imageSuhail
You have 2 ParseException variables, check the first one as wellRisorgimento
Logcat shows that exception as nullSuhail
Do I need to Shared Preferences or something like that ?Suhail
No, I don't think so, It should fetch the image each time the app opens.Risorgimento
Oh okay, this is really troublesome. I fiddle with the code a bit and queried for the image inside of onResume and when i reopen the app it did display the image but somehow I believe this isn't the best approachSuhail
S
0

The answer to this question is here: How To Create An App That Allows For Profile Picture Upload/Change?

I have resolved the problems I have been facing

Suhail answered 6/1, 2016 at 8:55 Comment(0)
C
0

Try using an image library that can handle picture caching.

You could try Glide or Picasso.

First add the library to your project. Them instead of downloading the file and decoding by yourself use the library to download and cache the object for you.

(Glide example) After searching for the object and extracting parsefile use Glide:

ParseFile fileObject = (ParseFile) object.get("imageContent");

String url = fileObject.getUrl();

ImageView image = (ImageView) findViewById(R.id.image);

Glide.with(getContext())
    .load(url)
    .into(image);

Next time you open the app Glide will search object in cache and only fetch if needed.

Colporteur answered 2/1, 2016 at 4:40 Comment(2)
Thanks for your effort. What happened was, the image was retrieved just the same but when I exited the app and reopen it the image was gone.Suhail
The image needs to be saved so that it can be seen again when the app is returned to. Just as how any social media apps operate (Twitter, Facebook, Instagram...) when you upload or change your profile image on those apps and save the changes if you press the back button or exit the app you can return to Twitter or IG or Facebook and still see your profile image in tact.Suhail
I
0

try to this way better to use BitMap.

ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("subclass");
    query.findInBackground(new FindCallback<ParseObject>() {

        @Override
        public void done(List<ParseObject> List, ParseException e) {
            // TODO Auto-generated method stub
            if (e == null) {
                // success
                if (List.size() > 0) {
                    for (ParseObject parseObject : List) {
                        // here fetch image like..
                        ParseFile image = (ParseFile) parseObject.get("fieldName");

                        if (image != null) {
                            Log.e("getting Image", "Image URL " + image.getUrl());
                            // here add to your adapter array
                            Picasso.with(getApplicationContext()).load(image.getUrl()).fit().into("imageView");
                        }

                    }
                    // and here call your adapter for fill array data
                }
            } else {
                // e.getMessage() here getting error ...
            }

        }
    });
Injustice answered 2/1, 2016 at 9:34 Comment(3)
I just want one image to be displayed. Not an entire list of imagesSuhail
For example....In the app, you can take or upload a pic, then that pic is set in an Image View, I would like the Picture you take or change to be saved in the image view and can always be seen later Just like how you upload your profile picture on FacebookSuhail
So I have the upload image to Parse working fine and the downloading image working fine BUT the image will not stay...when you exit the app and reopen it, the image is gone...Suhail
S
0

The answer to this question is here: How To Create An App That Allows For Profile Picture Upload/Change?

I have resolved the problems I have been facing

Suhail answered 6/1, 2016 at 8:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.