如何将照片保存到文件?

时间:2019-05-24 18:35:51

标签: java android

我想申请照相机。但是在星系S10e中,onActivityResult的数据为空。因此main无法将数据发送到结果。

https://github.com/DongWook0916/Allerger

Nexus 5、5X正常 Galaxy S10e不是正常

// 카메라 기능
private void selectPhoto() {
    String state = Environment.getExternalStorageState();
    // 외장메모리 검사
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        Intent intent_photo = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (intent_photo.resolveActivity(getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                Log.e("selectPhoto Error", ex.toString());
            }
            if (photoFile != null) {
                photoUri = FileProvider.getUriForFile(this, "com.example.allerger", photoFile);
                intent_photo.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
                startActivityForResult(intent_photo, CAMERA_CODE);
            }
        }
    } else {
        Toast.makeText(this, "저장공간이 접근 불가능한 기기입니다", Toast.LENGTH_SHORT).show();
        return;
    }
}

// 카메라로 찍은 사진 파일 생성
private File createImageFile() throws IOException {
    File dir = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/");
    if (!dir.exists()) {
        dir.mkdirs();
    }
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    mImageCaptureName = timeStamp + ".jpg";

    File storageDir = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/DCIM/Camera/"
            + mImageCaptureName);
    currentPhotoPath = storageDir.getAbsolutePath();
    return storageDir;
}

// 각 Intent 결과 처리
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            '''
            '''
            case CAMERA_CODE:
                if (data != null) {
                    Intent intent_result_camera = new Intent(MainActivity.this, ResultActivity.class);
                    intent_result_camera.putExtra("path", currentPhotoPath);
                    startActivity(intent_result_camera);
                } else{
                    Toast.makeText(this, "사진을 불러오지 못했습니다", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }
}

// 사진의 절대경로 구하기
private String getRealPathFromURI(Uri contentUri) {
    int column_index = 0;
    String[] pic = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(contentUri, pic, null, null, null);
    if (cursor.moveToFirst()) {
        column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    }
    return cursor.getString(column_index);
}

1)应用必须保存在DCIM / Camera / 2)应用程序必须将文件识别为图像 3)应用必须发送到ResultActivity.class

0 个答案:

没有答案