简体   繁体   English

在 Android 中使用 Parse.com 保存和检索图像文件

[英]Saving and Retrieving image filess using Parse.com in Android

I've been stuck at this whole day.我一直被困在这一天。 I've tried all other means available to accomplish this.我已经尝试了所有其他可用的方法来实现这一点。 This below approach almost did the work but it's giving a null pointer exception for the variable file.下面的方法几乎完成了工作,但它为变量文件提供了空指针异常。

The code below describes about saving data下面的代码描述了关于保存数据

            final ParseObject post = new ParseObject("Student");
            post.put("name", nameS);

            final ParseFile file = new ParseFile("photo.png", data);
            file.saveInBackground(new SaveCallback() {
                @Override
                public void done(ParseException e) {
                    post.put("studentPhoto", file);
                    Log.d("John", ""+ file);
                }
            });

            post.put("author", ParseUser.getCurrentUser());

            post.saveInBackground(new SaveCallback() {
                public void done(ParseException e) {
                    if (e == null) {
                        // Saved successfully.
                        Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(AddStudentActivityEditText.this, AddStudentActivityTextView.class);
                        startActivity(intent);

                    } else {
                        // The save failed.
                        Toast.makeText(getApplicationContext(), "Failed to Save", Toast.LENGTH_SHORT).show();
                        Log.d(getClass().getSimpleName(), "User update error: " + e);
                    }
                }
            });

Below data describes about Retrieving data下面的数据描述了关于检索数据

final ParseQuery<ParseObject> query = ParseQuery.getQuery("Student");
    query.whereEqualTo("author", ParseUser.getCurrentUser());
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {

                for (ParseObject text : objects) {

                    name.setText(text.getString("name"));
                 }

                ParseQuery<ParseObject> query = ParseQuery.getQuery("Student");
                query.whereEqualTo("author", ParseUser.getCurrentUser());
                query.getFirstInBackground(new GetCallback<ParseObject>() {
                    public void done(ParseObject object, ParseException e) {
                        if (object != null) {

                            ParseFile file = (ParseFile)object.get("studentPhoto");
                            file.getDataInBackground(new GetDataCallback() {


                                public void done(byte[] data, ParseException e) {
                                    if (e == null) {

                                        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                                        //use this bitmap as you want
                                        photo.setImageBitmap(bitmap);

                                    } else {
                                        // something went wrong
                                    }
                                }
                            });

                        } else {
                            Toast.makeText(getApplicationContext(), "Exception", Toast.LENGTH_SHORT) .show();

                        }
                    }
                });

            } else {
                Toast.makeText(getApplicationContext(), "Exception", Toast.LENGTH_SHORT).show();

            }
        }
    });

I don't know why your approach didn't working, but I will show you my approach for getting photos from parse.我不知道为什么你的方法不起作用,但我会向你展示我从解析中获取照片的方法。 First, make sure that you successfully saved your image - to do that, go to your app on parse.com panel, open Core tab, and check in your Student table if there is a column named studentPhoto, and there should be links to that photos.首先,确保您成功保存了您的图像 - 为此,请在 parse.com 面板上转到您的应用程序,打开 Core 选项卡,并检查您的 Student 表是否有名为 studentPhoto 的列,并且应该有指向该列的链接相片。 And my approach for getting image from parse is to use Picasso library.我从解析中获取图像的方法是使用毕加索库。 Simply, from parse you should get only link to photo, not whole file, and then download that photo using Picasso.简单地说,从解析你应该只获得照片的链接,而不是整个文件,然后使用毕加索下载该照片。 To get link to file do something like that:要获取文件链接,请执行以下操作:

ParseObject object = ... // your student object
try {
  object.fetchIfNeeded();
} catch (ParseException e) {
  e.printStackTrace();
}

ParseFile avatar  = object.getParseFile("studentPhoto");
String avatarUrl = avatar.getUrl();

And then set that image using Picasso:然后使用毕加索设置该图像:

   Picasso.with(getActivity()) // instead of getActivity() you could pass there any context
                    .load(avatarUrl)
                    .into(yourImageViewWithAvatar);

There is Picasso website for more info: http://square.github.io/picasso/有更多信息毕加索网站: http : //square.github.io/picasso/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM