0

在遵循本教程的一部分和SO 中这个问题的第二个答案之后,我设法将我从我的画廊中选择的一张照片保存到我在Parse中的对象中。

在此处输入图像描述

问题是我保存的照片具有.PNG扩展名(这只是一个屏幕截图)。当我试图从相机文件夹中选择一张普通照片时,没有保存任何内容并发生异常。

所有其他照片的扩展名是.jpg而 不是 .jpeg

正因为如此,我试图if发表声明,以便我可以检查照片的类型。以下代码的结果是,当我选择.JPG照片时,数据类型为NULL.

但是,我怎样才能将 .jpg 照片保存在我的 Parse Object 中?


在我的活动中,我有 2 个按钮。当您sign_in按下第一个(postData()通过对象解析。

第二个按钮是关于从图库中添加照片。在我的 .java 活动中,我有这个确切的监听器:

 picture.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), GET_FROM_GALLERY);
                }
            });

这是从onClick按钮的功能中调用的功能:

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


        //Detects request codes
        if(requestCode==GET_FROM_GALLERY && resultCode == Activity.RESULT_OK) {
            Uri selectedImage = data.getData();

            selectedImageType =data.getType();
            Toast.makeText(SignUpActivity.this, "Type: "+selectedImageType,
                    Toast.LENGTH_SHORT).show();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);

                // Convert it to byte
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // Compress image to lower quality scale 1 - 100
                if (selectedImageType == "JPEG"){
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

                    // bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    image = stream.toByteArray();
                }
                else if (selectedImageType == "JPG" || selectedImageType == "jpg"){
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

                    // bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    image = stream.toByteArray();
                }
                else  if (selectedImageType == "PNG") {
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

                    // bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    image = stream.toByteArray();
                }
                else{
                    Toast.makeText(SignUpActivity.this, "Please pick a JPEG or PNG photo!",
                            Toast.LENGTH_SHORT).show();
                }

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

这是保存数据的函数:

 public void postData(final String username,final String password,final String email,final String gender,final String age) {
        ParseObject user = new ParseObject("users");
        user.put("username", username);
        user.put("password", password);
        user.put("email", email);
        user.put("gender", gender);
        user.put("age_category", age);
        user.put("admin", false);

        ParseFile file = null;

        if (selectedImageType == "JPEG"){
            file = new ParseFile("profile_picture.jpeg", image);
        }
        else if (selectedImageType == "JPG" || selectedImageType == "jpg"){
            file = new ParseFile("profile_picture.jpg", image);
        }
        else if (selectedImageType == "PNG"){
            file = new ParseFile("profile_picture.png", image);
        }
        else{
            // Show a simple toast message
            Toast.makeText(SignUpActivity.this, "Please pick a JPEG or PNG photo!",
                    Toast.LENGTH_SHORT).show();
        }

        // Upload the image into Parse Cloud
        file.saveInBackground();

       user.put("photo", file);

        // Create the class and the columns
        user.saveInBackground();

        // Show a simple toast message
        Toast.makeText(SignUpActivity.this, "Image Uploaded",
                Toast.LENGTH_SHORT).show();


        Intent intent = new Intent(SignUpActivity.this, LoginActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);

        //finish();
    }
4

2 回答 2

1

所以 .jpeg 有效而 .jpg 无效?那么这个怎么样(注意你不应该用 == 比较字符串):

if (selectedImageType.toUpperCase().equals("JPEG") || selectedImageType.toUpperCase().equals("JPG")){
            file = new ParseFile("profile_picture.jpeg", image);
        }

您还可以合并一些早期的代码:

if (selectedImageType.toUpperCase().equals("JPEG") || selectedImageType.toUpperCase().equals("JPG")){
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    image = stream.toByteArray();
                }
于 2015-01-10T00:52:32.317 回答
1

删除试图在位图创建和图像发布到 parse.com 的整个过程中跟踪“selectedImageType”的 if 语句。

有了位图后,您可以简单地将所有压缩指定为“Bitmap.CompressFormat.JPEG”,然后简单地将所有 jpg 文件发布到 parse.com。

于 2015-01-18T17:14:42.787 回答