마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

Glide로 띄운 이미지 뷰를 갤러리에 저장 ( = 복사해서 갤러리에 넣기) 하고싶습니다.

0 추천

///////////////////////////
case R.id.iv_btn_download_addActivity:
                if(!verifyPermissions()){
                    Toast.makeText(getApplicationContext(), "권한이 없습니다!", Toast.LENGTH_SHORT).show();
                }else {

                    String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getString(R.string.app_name) + "/";

                    final File dir = new File(dirPath);

                    String imageURL = photoDataArrayList.get(position).getFileName(); 
                    final String fileName = imageURL.substring(imageURL.lastIndexOf('/') + 1);

                    BitmapDrawable bitmapDrawable = (BitmapDrawable) photoView.getDrawable();
                    Bitmap bitmap = bitmapDrawable.getBitmap();
                    Glide.with(this).asBitmap()
                            .load(photoDataArrayList.get(position).getFileName())
                            .into(new CustomTarget<Bitmap>(100, 100) {
                                @Override
                                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                    saveImage(bitmap,dir,fileName);
                                }

                                @Override
                                public void onLoadCleared(@Nullable Drawable placeholder) {

                                }

                                @Override
                                public void onLoadFailed(@Nullable Drawable errorDrawable) {
                                    super.onLoadFailed(errorDrawable);

                                    Toast.makeText(getApplicationContext(), "다운로드 실패!", Toast.LENGTH_SHORT).show();
                                }
                            });
                }
                break;


///////////////////////////

private void saveImage(Bitmap image, File storageDir, String imageFileName) {

        boolean successDirCreated = false;
        if (!storageDir.exists()) {
            successDirCreated = storageDir.mkdir();
        }
        if (successDirCreated) {
            File imageFile = new File(storageDir, imageFileName);
            String savedImagePath = imageFile.getAbsolutePath();
            try {
                OutputStream fOut = new FileOutputStream(imageFile);
                image.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
                fOut.close();
                Toast.makeText(getApplicationContext(), "Image Saved!", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Error while saving image!", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }

        }else{
            Toast.makeText(getApplicationContext(), "Failed to make folder!", Toast.LENGTH_SHORT).show();
        }
    }

이렇게 했는데 안 됩니다. ㅠ

제가 현재 glide로 이미지 뷰에 뿌려주는건 내부 스토리지 경로로 뿌려주는데
저장하는건 bitmap 과 url로 저장해야 하더라구요 참고할만한 레퍼런스가 있을까요?


 

jay_choi (530 포인트) 님이 2022년 11월 10일 질문

1개의 답변

0 추천
저장 후 파일이 보이지 않는 거라면 'MediaScanner' 관련 확인 해보시고

저장이 되지 않는 거라면 Android 11 부터 변경된 저장소 접근 관련 내용을 확인해보시기 바랍니다.
>> https://developer.android.com/about/versions/11/privacy/storage?hl=ko
익명 님이 2022년 11월 11일 답변
...