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

Java 在Android中使用Parse.com保存和检索图像文件,java,android,database,parse-platform,backend,Java,Android,Database,Parse Platform,Backend,我一整天都在忙这个。我已经尝试了所有其他方法来实现这一点。下面的方法几乎完成了工作,但它为变量文件提供了一个空指针异常 下面的代码描述了如何保存数据 final ParseObject post = new ParseObject("Student"); post.put("name", nameS); final ParseFile file = new ParseFile("photo.png", 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);
                    }
                }
            });
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();

            }
        }
    });
下面的数据描述了如何检索数据

            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);
                    }
                }
            });
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();

            }
        }
    });
final ParseQuery query=ParseQuery.getQuery(“学生”);
query.whereEqualTo(“author”,ParseUser.getCurrentUser());
findInBackground(新的FindCallback(){
@凌驾
公共void done(列出对象,parsee异常){
如果(e==null){
for(解析对象文本:对象){
name.setText(text.getString(“name”);
}
ParseQuery=ParseQuery.getQuery(“学生”);
query.whereEqualTo(“author”,ParseUser.getCurrentUser());
getFirstInBackground(新的GetCallback(){
公共无效完成(ParseObject对象,parsee异常){
if(对象!=null){
ParseFile file=(ParseFile)object.get(“studentPhoto”);
getDataInBackground(新的GetDataCallback(){
公共无效完成(字节[]数据,解析异常e){
如果(e==null){
位图位图=位图工厂.decodeByteArray(数据,0,数据.length);
//根据需要使用此位图
photo.setImageBitmap(位图);
}否则{
//出了点问题
}
}
});
}否则{
Toast.makeText(getApplicationContext(),“Exception”,Toast.LENGTH\u SHORT.show();
}
}
});
}否则{
Toast.makeText(getApplicationContext(),“Exception”,Toast.LENGTH\u SHORT.show();
}
}
});

我不知道为什么您的方法不起作用,但我将向您展示我从parse获取照片的方法。 首先,确保您成功保存了图像-为此,请转到parse.com面板上的应用程序,打开Core选项卡,并在学生表中检查是否有名为studentPhoto的列,并且应该有指向该照片的链接。 我从解析中获取图像的方法是使用毕加索库。简单地说,从解析中,您应该只获得照片的链接,而不是整个文件,然后使用毕加索下载该照片。 要获取文件链接,请执行以下操作:

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

ParseFile avatar  = object.getParseFile("studentPhoto");
String avatarUrl = avatar.getUrl();
然后使用毕加索设置该图像:

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

有毕加索的网站提供更多信息:

有学生栏,但没有任何链接,有一些东西,类型是字节。嗯,应该有学生表,而不是列。表中应该有一个名为studentPhoto的列。是的,没错……但是没有任何链接,它的类型是byte……我稍后会试试你的方法,让你知道