问题
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.");
}
}
});
}
});
}
});
}
}
回答1:
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.
回答2:
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 ...
}
}
});
回答3:
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
来源:https://stackoverflow.com/questions/34561551/an-image-downloaded-from-parse-stay-on-screen-even-after-you-exit-and-reopen-app