簡體   English   中英

Android如何從圖庫中保存圖像以進行解析?

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

我正在開發一個應用程序,並且用戶有一個配置文件,我可以存儲名稱和姓氏,但是我不知道該如何存儲圖像,用戶可以從圖庫中選擇。 我只有選擇圖像並將其設置為imageView的代碼。 有任何想法嗎?

這是代碼:

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();
    }

}

我也閱讀了本教程,但不是我想要的

嘗試這個。 image-chooser-library獲取圖像表單庫和相機

   private File destination_bg;

在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());
        }

在保存按鈕上,您可以使用此

        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