Android

Android 카메라 / 갤러리 저장하기 ( Q Scoped Storage)

Machine_웅 2021. 3. 12. 14:30
728x90
반응형

Manifesats

 

    <uses-permission android:name="android.permission.CAMERA" />

    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="28" />
    <uses-permission
        android:name="android.permission.READ_EXTERNAL_STORAGE"
        android:maxSdkVersion="28" />

    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

 

       <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.appgrider.test_android.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
        

 

res - xml - file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external"
        path="." />
    <cache-path
        name="cache"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <files-path
        name="files"
        path="." />

    <external-files-path name="external_files" path="Android/data/com.appgrider.test_android/files/Pictures" />
</paths>

data/ 본인의 패키지명 / files / 디렉터리명

 

 

// 퍼미션은 있다고 가정하겠습니다.

 

 

// 카메라 열기

    private void camera_open_intent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                Log.e("Woongs",ex.getMessage().toString());
            }

            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri providerURI = FileProvider.getUriForFile( this , "com.appgrider.test_android.fileprovider", photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, providerURI);
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }
    

 

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Uri test = Uri.fromFile(new File(currentPhotoPath));
            saveFile(test);
        }
    }

currentPhotoPath  는  위의 createImageFile() 로 파일을 만들때 절대경로를 저장한 String 변수입니다.

 

 

// 파일 생성

   // 파일 생성
    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        // 참고: getExternalFilesDir() 또는 getFilesDir()에서 제공한 디렉터리에 저장한 파일은 사용자가 앱을 제거할 때 삭제됩니다.

        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );
        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = image.getAbsolutePath();

        return image;
    }

 

// 카메라로 찍은사진 갤러리에 저장 

 // 파일 저장
    private void saveFile(Uri image_uri) {

        ContentValues values = new ContentValues();
        String fileName =  "woongs"+System.currentTimeMillis()+".png";
        values.put(MediaStore.Images.Media.DISPLAY_NAME,fileName);
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/*");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            values.put(MediaStore.Images.Media.IS_PENDING, 1);
        }

        ContentResolver contentResolver = getContentResolver();
        Uri item = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        try {
            ParcelFileDescriptor pdf = contentResolver.openFileDescriptor(item, "w", null);
            if (pdf == null) {
                Log.d("Woongs", "null");
            } else {
                byte[] inputData = getBytes(image_uri);
                FileOutputStream fos = new FileOutputStream(pdf.getFileDescriptor());
                fos.write(inputData);
                fos.close();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    values.clear();
                    values.put(MediaStore.Images.Media.IS_PENDING, 0);
                    contentResolver.update(item, values, null, null);
                }

                // 갱신
                galleryAddPic(fileName);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.d("Woongs", "FileNotFoundException  : "+e.getLocalizedMessage());
        } catch (Exception e) {
            Log.d("Woongs", "FileOutputStream = : " + e.getMessage());
        }
    }

 

 

// 그밖에 유틸

    // Uri to ByteArr
    public byte[] getBytes(Uri image_uri) throws IOException {
        InputStream iStream = getContentResolver().openInputStream(image_uri);
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
        int bufferSize = 1024; // 버퍼 크기
        byte[] buffer = new byte[bufferSize]; // 버퍼 배열

        int len = 0;
        // InputStream에서 읽어올 게 없을 때까지 바이트 배열에 쓴다.
        while ((len = iStream.read(buffer)) != -1)
            byteBuffer.write(buffer, 0, len);
        return byteBuffer.toByteArray();
    }

 

// 갤러리 갱신

    private void galleryAddPic(String Image_Path) {

        Log.d("Woongs","갱신 : "+Image_Path);

        // 이전 사용 방식
        /*Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(Image_Path);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.context.sendBroadcast(mediaScanIntent);*/
        
        File file = new File(Image_Path);
        MediaScannerConnection.scanFile(context,
                new String[]{file.toString()},
                null, null);
    }

 

// 갤러리 호출

    private void gallery_open_intent(){
        Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent,REQUEST_GALLERY_IMAGE);
    }

 

 

//------------------------------------  퍼미션 -------------------------------------------------------------------

 

방법1 

    public void checkPermission(){
        String[] permission_list = {
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
        };

        //현재 안드로이드 버전이 6.0미만이면 메서드를 종료한다.
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
            return;

        for(String permission : permission_list){
            //권한 허용 여부를 확인한다.
            int chk = checkCallingOrSelfPermission(permission);
            if(chk == PackageManager.PERMISSION_DENIED){
                //권한 허용을여부를 확인하는 창을 띄운다
                requestPermissions(permission_list,0);
            }
        }
    }

방법2)

public void checkPermission2(Context context,Activity activity){
        if(
                ContextCompat.checkSelfPermission(context,Manifest.permission.CAMERA)+
                ContextCompat.checkSelfPermission(context,Manifest.permission.READ_EXTERNAL_STORAGE)+
                ContextCompat.checkSelfPermission(context,Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED
        ){
            if(ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.CAMERA)
             || ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.READ_EXTERNAL_STORAGE)
             || ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.WRITE_EXTERNAL_STORAGE)
            ){
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("권한요청");
                builder.setMessage("카메라, 폴더에 접근 요청");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions(activity,
                                new String[]{
                                        Manifest.permission.CAMERA,
                                        Manifest.permission.READ_EXTERNAL_STORAGE,
                                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                                },0000
                        );
                    }
                });
                builder.setNegativeButton("NO",null);
                AlertDialog alertDialog = builder.create();
                alertDialog.show();

            }else{
                ActivityCompat.requestPermissions(activity,
                        new String[]{
                                Manifest.permission.CAMERA,
                                Manifest.permission.READ_EXTERNAL_STORAGE,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE
                        },0000
                );
            }
        }else{
            // 이미 퍼미션이 있는 경우
            Toast.makeText(context,"퍼미션이 이미 있음",Toast.LENGTH_SHORT).show();
        }
    }
728x90
반응형