简体   繁体   English

Android如何从图库中保存图像以进行解析?

[英]Android How to save an image from gallery to parse?

I'm developing an app and the user have a profile, i'm able to store the name and lastname but I can't figure it out how to store the image, the user can pick it from the gallery. 我正在开发一个应用程序,并且用户有一个配置文件,我可以存储名称和姓氏,但是我不知道该如何存储图像,用户可以从图库中选择。 I only have the code for choose the image and set it to an imageView. 我只有选择图像并将其设置为imageView的代码。 Any ideas? 有任何想法吗?

Here's the code: 这是代码:

public void loadImagefromGallery(View view) {
    // Create intent to Open Image applications like Gallery, Google Photos
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // Start the Intent
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // When an Image is picked
        if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                && null != data) {
            // Get the Image from data

            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            // Get the cursor
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imgDecodableString = cursor.getString(columnIndex);
            cursor.close();
            ImageView imgView = (ImageView) findViewById(R.id.imageViewFotoPerfil);
            // Set the Image in ImageView after decoding the String
            imgView.setImageBitmap(BitmapFactory
                    .decodeFile(imgDecodableString));

        } else {
            Toast.makeText(this, "No has elegido una imagen",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Algo salió mal", Toast.LENGTH_LONG)
                .show();
    }

}

Also I read this tutorial but is not what I want 我也阅读了本教程,但不是我想要的

Try this. 尝试这个。 image-chooser-library To get Image form Gallery and Camera image-chooser-library获取图像表单库和相机

   private File destination_bg;

In onImageChosen() method you set this code. 在onImageChosen()方法中,您可以设置此代码。

   Glide.with(getActivity()).load(image.getFileThumbnail()).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).into(img_userProfilePicture_bg);
        if (image.getFileThumbnail() != null) {
            destination_bg = new File(image.getFileThumbnail());
            Log.i("test", "test" + image.getFileThumbnail());
        }

In On saveButton you can use this 在保存按钮上,您可以使用此

        if (destination_bg != null) {
            Glide.with(getActivity()).load(destination_bg.getAbsolutePath()).asBitmap().toBytes().centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).into(new SimpleTarget<byte[]>() {
                @Override
                public void onResourceReady(byte[] resource, GlideAnimation<? super byte[]> glideAnimation) {

                    final ParseFile parseFile = new ParseFile(destination_bg.getName(), resource);
                    parseFile.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(ParseException e) {

                            //backgroundPicture is table coulmn name where i save my image.

                            currentUser.put("backgroundPicture", parseFile);
                            currentUser.saveInBackground(new SaveCallback() {
                                @Override
                                public void done(ParseException e) {
                                    showToast("Background image upload success");
                                }
                            });
                        }
                    });


                }
            });
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM