【问题标题】:Saving images to Parse将图像保存到 Parse
【发布时间】:2014-08-13 20:05:56
【问题描述】:

我试图让用户将他们的图片(个人资料图片)上传到解析中,作为他们的个人资料创建页面的一部分,他们必须在其中填写各种信息。用户将能够在图像视图中预览他们的图片。

我已尝试通过以下步骤来实现这一目标。

1) 创建一个按钮,允许用户从他们的图库中检索图片,并将其上传到创建的 ImageView 中。

Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
        buttonLoadImage.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);
            }
        });
@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                    && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            }

        }
        } 

这部分通常已成功执行,用户可以从他们的画廊上传图片并在图片视图中直观地看到它。

2) 在解析时存储用户上传的图像 这是我苦苦挣扎的领域。在代码之间添加了注释以进一步澄清,并突出显示我的问题。

 ParseUser currentUser = ParseUser.getCurrentUser();

                 /* This is the section where the images is converted, saved, and uploaded. I have not been able Locate the image from the ImageView, where the user uploads the picture to imageview from either their gallery and later on from facebook */ 
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                        /*to be retrieved from image view */);
                // 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("profilePicture.png", image);
                // Upload the image into Parse Cloud


                // Create a column named "Profile Picture" and set the string
                currentUser.put("ImageName", "Profile Picture");

                // Create a column named "ImageFile" and insert the image
                currentUser.put("ProfilePicture", file);

                // Create the class and the columns

                currentUser.put("name", name); 
                currentUser.put("age", age); 
                currentUser.put("headline", headline); 
                currentUser.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        setProgressBarIndeterminateVisibility(false);

                        if (e == null) {
                            // Success!
                            Intent intent = new Intent(ProfileCreation.this, MoodActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                        }
                        else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
                            builder.setMessage(e.getMessage())
                                .setTitle(R.string.signup_error_title)
                                .setPositiveButton(android.R.string.ok, null);
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                    }
                });
            }
        }
    });

如果您无论如何都可以帮助我,那将是非常有帮助的。 如果您需要进一步澄清,请告诉我。 提前致谢。

【问题讨论】:

    标签: java android android-layout android-activity parse-platform


    【解决方案1】:

    你忘了先保存文件

    ParseFile file = new ParseFile("profilePicture.png", image);
    file.saveinBackground()
    

    【讨论】:

    • 感谢您的及时回复和后台保存图片的提醒。我在完成这些代码行时也遇到了很大的问题:“Bitmap bitmap = BitmapFactory.decodeResource(getResources(), /*to be retrieved from image view */);”我希望 getResources 不是从我的可绘制对象中检索,而是从 imageview 中检索,其中 imageview 的图片正在从用户图像库中显示(很快用户也可以将图像从 facebook 上传到他们的 imageview 中)。
    【解决方案2】:

    您可以定义一个名为 util.java 的新类,并在该类中编写一个方法

     public static byte[] getbytearray(Bitmap bm){
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
          bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
          byte[] byteArray = stream.toByteArray();
          return byteArray;
      }
    

    现在您不需要一次又一次地使用这些基本行。 更重要的是,您应该使用 asynctask 来避免内存不足错误,例如 -

    public class storeimage extends AsyncTask<Bitmap,Void,Void>{
    
            @Override
            protected Void doInBackground(Bitmap... params) {
                byte[] byteArray = util.getbytearray(params[0]);
                ParseUser user = ParseUser.getCurrentUser();
                ParseFile parseFile = new ParseFile(user.getUsername()+"dp.png",byteArray);
                parseFile.saveInBackground();
                user.put("dp", parseFile);
                user.saveInBackground();
                Log.d("mayank","asynctask successful");
    
              //  Bitmap dpbitmap2 = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
                //ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
              //  dpbitmap.compress(Bitmap.CompressFormat.PNG, 0, stream2);
               // byte[] byteArray2 = stream2.toByteArray();
               // ParseFile parseFile2 = new ParseFile(user.getUsername()+"dp_circle.png",byteArray2);
               // ParseObject dp = new ParseObject("dp");
               // dp.put("pic",byteArray2);
               // dp.pinInBackground();
                return null;
            }
        }
    

    之后
    你应该写

     new storeimage().execute(BitmapFactory.decodeFile(picturePath));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多