【问题标题】:How to Upload Gallery Image on parse.com in Android?如何在 Android 的 parse.com 上上传图库图片?
【发布时间】:2015-04-22 10:04:54
【问题描述】:

我创建了一个 Android 应用程序,其中存储了 parse.com 上的用户名、电话、密码、纬度和经度。当我单击提交按钮时,所有这些值都成功存储在 parse.com 上。这是代码

LoginSigupActivity.java

    public void onClick(View arg0) {
                    usernametxt = username.getText().toString();
                    passcodetxt = passcode.getText().toString();
                    phonetxt = phone.getText().toString();

                    if (usernametxt.equals("") && passcodetxt.equals("")) {
                        Toast.makeText(getApplicationContext(),
                                "Please complete the signup form",
                                Toast.LENGTH_LONG).show();

                    } else {
                        // Save new user data into Parse.com Data Storage
                        ParseUser user = new ParseUser();
                        user.setUsername(usernametxt);
                        user.setPassword(passcodetxt);
                        user.put("phone", phone.getText().toString());
                        user.put("passcode", passcode.getText().toString());
                        //user.put("profile_pic", "my profile pic");

                        // code to send the current latlong using parse.com
                        user.put("latitude", gpsTracker.getLatitude() + "");
                        user.put("longitude", gpsTracker.getLongitude() + "");
                        
                        
                        user.signUpInBackground(new SignUpCallback() {
                            public void done(ParseException e) {
                                if (e == null) {

                                    Intent intent = new Intent(
                                            LoginSignupActivity.this, Welcome.class);
                                    startActivity(intent);

                                    Toast.makeText(getApplicationContext(),
                                            "Successfully Signed up !!!",
                                            Toast.LENGTH_LONG).show();
                                } else {
                                    Toast.makeText(getApplicationContext(),
                                            "Sign up Error", Toast.LENGTH_LONG)
                                            .show();
                                }
                            }
                        });
                    }

                }

            });

        }
    }

现在,当第二个活动在这里打开时,我使用了图像视图、获取图像按钮和上传图像按钮。单击获取图像按钮图像在图像视图中正确获取,但是当我尝试上传时,它不会上传。

代码如下:

Welcome.java

    selectImageButton = (Button) findViewById(R.id.selectImageButton);
            // initlize gps tracker
            gpsTracker = new GPSTracker(this);

            String picturePath = PreferenceManager
                    .getDefaultSharedPreferences(this).getString("picturePath", "");
            if (!picturePath.equals("")) {
                ImageView imageView = (ImageView) findViewById(R.id.uploadImage);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            }

            selectImageButton = (Button) findViewById(R.id.selectImageButton);
            selectImageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    Intent i = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(i, RESULT_LOAD_IMAGE);
                }
            });
                    

           uploadButton = (Button) findViewById(R.id.uploadButton);

            uploadButton.setOnClickListener(new View.OnClickListener() {
                @SuppressWarnings("null")
                @Override
                public void onClick(View arg0) {  
                    
                    ParseUser user = new ParseUser();
                    Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
                    // Convert it to byte
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    // Compress image to lower quality scale 1 - 100
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    byte[] image = stream.toByteArray();

                    // Create the ParseFile
                    ParseFile file = new ParseFile("androidbegin.png", image);
                    // Upload the image into Parse Cloud
                    file.saveInBackground();

            
                    });*/

                    user.signUpInBackground(new SignUpCallback() {
                        public void done(ParseException e) {
                            if (e == null) {
                                Toast.makeText(getApplicationContext(),
                                        "Successfully Signed up !!!",
                                        Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(getApplicationContext(),
                                        "Sign up Error", Toast.LENGTH_LONG).show();
                            }
                        }
                    });

                }
            });

            // Retrieve current user from Parse.com
            ParseUser currentUser = ParseUser.getCurrentUser();
           // Convert currentUser into String
            String struser = currentUser.getUsername().toString();
           // Locate Button in welcome.xml
            logout = (Button) findViewById(R.id.logout);
            // Logout Button Click Listener
            logout.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    Intent intent = new Intent(Welcome.this,
                            LoginSignupActivity.class);
                    startActivity(intent);
                }
            });
        }      
      
       

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

            if (resultCode == RESULT_OK) {

                Uri selectedImageUri = data.getData();

                // GET IMAGE PATH
                imagePath = getPath(selectedImageUri);

                // IMAGE NAME
                imageName = imagePath.substring(imagePath.lastIndexOf("/"));

                // Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
                //
                // // DISPLAY IMAGE
                // imageView.setImageBitmap(bitmap);
                // imageLocationTextView.setText("File path :" + imagePath);
            }
        }

        private String getPath(Uri uri) {
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
    }

现在我想知道如何从 parse.com 的图库中上传选定的图像,因为我的其他参数保存在 parse 中。我也很困惑,因为我从我的第一个活动上传了用户名、密码、纬度、经度和电话。但我在第二个活动中从图库中获取图像。

现在我想知道如何在 parse.com 的同一类中上传所选图像(保存第一个活动参数的位置)。 User 是我在 parse.com 中的类名。

【问题讨论】:

  • 您将文件存储在 parse.com 的什么位置?
  • 我已将文件存储在 User 类的 profile_pic 列下。
  • 在您的代码中,您告诉 parse.com 将 parsefile 对象存储在 profile_pic 中的什么位置?
  • 在 else 条件下的第一个活动中,比如这个 user.put("profile_pic", "my profile pic");
  • 您的代码正确吗?您在该行中存储字符串,那么如何存储图像?

标签: android parse-platform image-gallery


【解决方案1】:

检查下面的代码,你会明白的

ParseObject  ParseObj = new ParseObject ("YOUR_TABLE_NAME");
ParseFile file = new ParseFile("androidbegin.png", image);
ParseObj.put("profile_pic", file);
ParseObj.saveInBackground(new SaveCallback() {
    @Override
    public void done(ParseException arg0) {
        if (arg0 == null) {
            Log.i("LOG_OUTPUT", "Data saved in sever");
        } else {
            Log.e("LOG_OUTPUT", "Data not saved in server");

        }
    }
});
file.saveInBackground(new SaveCallback() {
    @Override
    public void done(ParseException arg0) {
        try {
            if (arg0 == null) {
                Log.e("LOG_OUTPUT", "imageFile is saved");
            } else {
                Log.e("LOG_OUTPUT", "imageFile is not saved");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

});

【讨论】:

    【解决方案2】:

    这对我有用

                           imageView1.buildDrawingCache();
                            Bitmap bitmap =imageView1.getDrawingCache();
                                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                                // Compress image to lower quality scale 1 -
                                // 100
                                bitmap.compress(Bitmap.CompressFormat.PNG,
                                        100, stream);
                                byte[] image = stream.toByteArray();
    
                                // Create the ParseFile
                                ParseFile file = new ParseFile(
                                        "Profile.png", image);
    
                                // file.getUrl();
                                // Upload the image into Parse Cloud
                                file.saveInBackground();
    
                                // Create a New Class called "ImageUpload"
                                // in Parse
                                final ParseObject imgupload = new ParseObject(
                                        "ImageUpload");
    
                                // Create a column named "ImageName" and set
                                // the string
                                imgupload.put("ImageName",
                                        "Profile Picture");
    
                                // Create a column named "ImageFile" and
                                // insert the image
                                imgupload.put("ImageFile", file);
    
                                // Create the class and the columns
                                imgupload.saveInBackground();
    
                                imgupload
                                        .saveInBackground(new SaveCallback() {
                                            @Override
                                            public void done(
                                                    ParseException e) {
                                                if (e == null) {
                                                    // Success!
    
                                                    String objectId = imgupload
                                                            .getObjectId();
                                                    Editor editor = sharedpref_objid
                                                            .edit();
                                                    editor.putString(
                                                            "objid",
                                                            objectId);
    
                                                    Toast.makeText(
                                                            getApplicationContext(),
                                                            objectId,
                                                            Toast.LENGTH_SHORT)
                                                            .show();
    
                                                    editor.commit();
    
                                                } else {
                                                    // Failure!
                                                }
                                            }
                                        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-20
      • 2012-03-07
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 2012-01-25
      • 1970-01-01
      相关资源
      最近更新 更多