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

 

<queries>
    <!-- Camera -->
    <intent>
        <action android:name="android.media.action.IMAGE_CAPTURE" />
    </intent>
    
    <!-- Gallery -->
    <intent>
        <action android:name="android.intent.action.GET_CONTENT" />
    </intent>
</queries>

 

[xxxx_provider.xml] 

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path
        name="files"
        path="."/>
    <external-files-path
        name="external_files_path"
        path="." />
    <external-path
        name="external_files"
        path="."/>
</paths>

 

[AndroidManifest.xml] 에 등록해줘야됨.

<application
		android:name=".App"
        android:allowBackup="false"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:requestLegacyExternalStorage="true"
        android:supportsRtl="true"
        android:theme="@style/Theme.xxxxxxxx"
        tools:targetApi="31">
        			.... 생략 ....
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="kr.xxx.xxxxxxx.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/xxxx_provider" />
        </provider>
           			.... 생략 ....
</application>

 

[안드로이드 OS 카메라 데이터 받기]

@Override
protected void onCreate(Bundle saveInstanceState) {
			....... 생략 .......
     ActivityResultLauncher<Intent> mCameraResultLanucher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if (result.getResultCode() == RESULT_OK) {
                            Bitmap bitmap;
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { // >= 30
                                String contentPath = PreferenceManager.getString(MainActivity.this,PreferencesKey.CAMERA_URI);
                                Uri uri = Uri.parse(contentPath);
                                try {
                                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { //<28
                                        bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                                    } else {
                                        ImageDecoder.Source source = ImageDecoder.createSource(getContentResolver(), uri);
                                        bitmap = ImageDecoder.decodeBitmap(source);
                                    }

                                    if (bitmap == null) {
                                        Log.e("mCameraResultLanucher Error", "bitmap null!!");
                                        return;
                                    }
                                } catch (IOException e) {
                                    Log.e("mCameraResultLanucher IOException", e.getMessage());
                                }
                            } else {
                                Intent intentData = result.getData();
                                Bundle extras = intentData.getExtras();
                                bitmap = (Bitmap) extras.get("data");
                                if (bitmap == null) {
                                    Log.e("mCameraResultLanucher Error", "bitmap null!!");
                                    return;
                                }
                            }

							//bitmap 이미지 처리 
                        }
                        if (result.getResultCode() == RESULT_CANCELED) {
                            //ToDo
                            Toast.makeText(MainActivity.this, "카메라 촬영을 취소했습니다.", Toast.LENGTH_LONG).show();

                            /**
                             * 시스템 카메라에서 사진을 찍고 x 버튼을 누르거나 back버튼으로 취소시 생성한 파일을 삭제한다.
                             * 삭제하지 않으면 갤러리에 빈파일이 남는다.
                             */
                            String contentPath = PreferenceManager.getString(MainActivity.this,PreferencesKey.CAMERA_URI);
                            Uri uri = Uri.parse(contentPath);
                            if (uri != null) {
                                getContentResolver().delete(uri, null, null);
                            }
                        }
                    }
                }
        );
}

 

 

[안드로이드 OS 카메라 호출]

 

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { // >= 30
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, "New Picture");
    values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");

    Uri photoUri = getContext().getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values
    );
	
    // 이미지 url 임시 저장소 (게시물에 PreferenceManager 클래스 참조)
    // 임시로 photoUri 멤버변수로 사용해도됨. 
    PreferenceManager.setString(getContext(), PreferencesKey.CAMERA_URI, String.valueOf(photoUri));
    intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);

    mCameraResultLanucher.launch(intent);

} else {  // <= 29
    intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (intent.resolveActivity(getContext().getPackageManager()) != null) {

        mCameraResultLanucher.launch(intent);
    }
}

+ Recent posts