이
전체 Source.
요청을 여기에 AndroidManifest.xml
에이 권한 봐 :
static final int REQUEST_IMAGE_CAPTURE = 1;
private Bitmap mImageBitmap;
private String mCurrentPhotoPath;
private ImageView mImageView;
는 그런 다음 onClick
이 Intent
화재 : 당신의 Activity
에
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
을,이 정의에 의해 시작 :
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.i(TAG, "IOException");
}
// Continue only if the File was successfully created
if (photoFile != null) {
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
는 다음과 같은 지원 방법을 추가
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 = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, // prefix
".jpg", // suffix
storageDir // directory
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
가 그런 결과가 나타납니다
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
mImageView.setImageBitmap(mImageBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
이 작품은 developer.android.com의 코드와는 다른 MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath))
입니다 만들 었는지. 원래 코드는 나에게 FileNotFoundException
을주었습니다.
임시 파일에 저장 한 다음 서버로 전송을 마치면 삭제할 수 있습니다. –
@SohailZahid 도와 주셔서 감사합니다 !! 어떻게 임시 파일에 저장할 수 있습니까? –
@Sohail Zahid, 도와 주셔서 감사합니다 !! 어떻게 임시 파일에 저장할 수 있습니까? –