从Parse中的用户类获取图像

时间:2015-07-17 18:12:30

标签: android image parse-platform

我正在尝试从Parse中的用户类获取文件(配置文件图像)。 这是我正在尝试的代码但是我收到以下错误,我哪里出错?

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(mToolbar);

        navigationItemsList = new ArrayList<DrawerItem>();
        addItemsToNavigationList();


        mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
        mRecyclerView.setHasFixedSize(true);

        mAdapter = new com.charity.dogood.dogood.adapters.NavDrawerAdapter(navigationItemsList, this, HEADER_NAME, HEADER_EMAIL, HEADER_IMAGE);
        mRecyclerView.setAdapter(mAdapter);


        //Parse Image and details of the logged in user
        // Locate the class table named "ImageUpload" in Parse.com
        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("User");
        Log.d("Mainactivity", "After query 1");

        ParseUser currentUser = ParseUser.getCurrentUser();
        Log.d("Mainactivity", "After query 2");
        query.whereEqualTo("username", currentUser);
        Log.d("Mainactivity", "After query 3");
        // String usernam = (String)currentUser;

        if(currentUser != null){
            Log.d("Mainactivity", "came to current user check statement");
        query.getInBackground(currentUser.getUsername().toString(), new GetCallback<ParseObject>() {

            @Override
            public void done(ParseObject object, ParseException e) {

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

                fileObject.getDataInBackground(new GetDataCallback() {
                    @Override
                    public void done(byte[] bytes, ParseException e) {
                        if (e == null) {
                            Toast.makeText(MainActivity.this, "Welcome Back", Toast.LENGTH_LONG).show();
                            Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

                            // HEADER_IMAGE image = (ImageView) fin
                        }
                        else
                        {
                            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                            builder.setMessage(e.getMessage());
                            builder.setTitle("Sorry");
                            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                }
                            });
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }

                    }
                });

            }
        });
            Log.d("Mainactivity", "After query 4");
    }

ERROR

07-17 14:11:40.030: D/Mainactivity(3857): After query 1
07-17 14:11:40.030: D/Mainactivity(3857): After query 2
07-17 14:11:40.030: D/Mainactivity(3857): After query 3
07-17 14:11:40.030: D/Mainactivity(3857): came to current user check statement
07-17 14:11:40.030: D/Mainactivity(3857): After query 4
07-17 14:11:40.120: V/WindowManager(463): Adding window Window{267de77b u0 com.charity.dogood.dogood/com.charity.dogood.dogood.activities.MainActivity} at 3 of 9 (after Window{3da1ef5a u0 com.charity.dogood.dogood/com.charity.dogood.dogood.activities.Login})
07-17 14:11:40.220: W/EGL_emulation(3857): eglSurfaceAttrib not implemented
07-17 14:11:40.220: W/OpenGLRenderer(3857): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa207d700, error=EGL_SUCCESS
07-17 14:11:40.276: I/ActivityManager(463): Displayed com.charity.dogood.dogood/.activities.MainActivity: +283ms
07-17 14:11:40.421: D/AndroidRuntime(3857): Shutting down VM
07-17 14:11:40.421: E/AndroidRuntime(3857): FATAL EXCEPTION: main
07-17 14:11:40.421: E/AndroidRuntime(3857): Process: com.charity.dogood.dogood, PID: 3857
07-17 14:11:40.421: E/AndroidRuntime(3857): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object com.parse.ParseObject.get(java.lang.String)' on a null object reference
07-17 14:11:40.421: E/AndroidRuntime(3857):     at com.charity.dogood.dogood.activities.MainActivity$1.done(MainActivity.java:104)
07-17 14:11:40.421: E/AndroidRuntime(3857):     at com.charity.dogood.dogood.activities.MainActivity$1.done(MainActivity.java:99)
07-17 14:11:40.421: E/AndroidRuntime(3857):     at com.parse.Parse$6$1.run(Parse.java:944)
07-17 14:11:40.421: E/AndroidRuntime(3857):     at android.os.Handler.handleCallback(Handler.java:739)
07-17 14:11:40.421: E/AndroidRuntime(3857):     at android.os.Handler.dispatchMessage(Handler.java:95)
07-17 14:11:40.421: E/AndroidRuntime(3857):     at android.os.Looper.loop(Looper.java:135)
07-17 14:11:40.421: E/AndroidRuntime(3857):     at android.app.ActivityThread.main(ActivityThread.java:5254)
07-17 14:11:40.421: E/AndroidRuntime(3857):     at java.lang.reflect.Method.invoke(Native Method)
07-17 14:11:40.421: E/AndroidRuntime(3857):     at java.lang.reflect.Method.invoke(Method.java:372)
07-17 14:11:40.421: E/AndroidRuntime(3857):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-17 14:11:40.421: E/AndroidRuntime(3857):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

更新:

 @Override
            public void done(ParseObject object, ParseException e) {


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


                if(fileObject != null) {
                    fileObject.getDataInBackground(new GetDataCallback() {

                        @Override
                        public void done(byte[] bytes, ParseException e) {
                            if (e == null) {
                                Toast.makeText(MainActivity.this, "Welcome Back", Toast.LENGTH_LONG).show();
                                Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

                                // HEADER_IMAGE image = (ImageView) fin
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                                builder.setMessage(e.getMessage());
                                builder.setTitle("Sorry");
                                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        dialogInterface.dismiss();
                                    }
                                });
                                AlertDialog dialog = builder.create();
                                dialog.show();
                            }

                        }
                    });
                }
                else
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setMessage(e.getMessage());
                    builder.setTitle("Sorry");
                    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();

                }

1 个答案:

答案 0 :(得分:1)

鉴于用户已登录,这对我有用。

    private void setUserImage() {
    ImageView imgUser = (ImageView) findViewById(R.id.yourImage);
    ParseUser currentUser = ParseUser.getCurrentUser();
            try {
                    ParseFile img = currentUser.getParseFile("userImage");
                    Bitmap bmp = BitmapFactory.decodeStream(img.getDataStream());
                    imgUser.setImageBitmap(bmp);
            } catch (Exception e) {
                    e.printStackTrace();
            }
    }