即使在您退出并重新打开应用程序后,从 Parse 下载的图像仍保留在屏幕上?

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

我有一个应用程序可以从 Parse.com 下载图像并在图像视图中显示该图像

问题是每当我退出应用程序(使用后退按钮)并且 return 图像就消失了。

怎样才能让图片留下来?

(例如:当您在 Twitter 上更新个人资料照片并离开应用程序时,return您的个人资料照片仍会显示)

任何帮助将不胜感激,这非常重要。

主要活动:

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

        });
    }
}

尝试使用可以处理图片缓存的图片库。

你可以试试Glide or Picasso

首先将库添加到您的项目中。他们不是自己下载文件和解码,而是使用库为您下载和缓存对象。

(滑行示例) 搜索对象并提取解析文件后使用 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);

下次打开应用程序时,Glide 将在缓存中搜索对象并仅在需要时获取。

尝试这种方式更好地使用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 ...
            }

        }
    });

这个问题的答案在这里:

我已经解决了我一直面临的问题