I use the following codes to launch the camera from my app:
private void saveFullImage() {
String storageState = Environment.getExternalStorageState();
if (storageState.equals(Environment.MEDIA_MOUNTED)) {
String path = Environment.getExternalStorageDirectory().getName()
+ File.separatorChar + "Android/data/"
+ RegistrationDetails.this.getPackageName() + "/files/"
+ md5("filedummy") + ".jpg";
File photoFile = new File(path);
try {
if (photoFile.exists() == false) {
photoFile.getParentFile().mkdirs();
photoFile.createNewFile();
}
} catch (IOException e) {
Log.e(TAG, "Could not create file.", e);
}
Log.i(TAG, path);
Uri fileUri = Uri.fromFile(photoFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, TAKE_PICTURE);
} else {
new AlertDialog.Builder(this)
.setMessage(
"External Storeage (SD Card) is required.\n\nCurrent state: "
+ storageState).setCancelable(true)
.create().show();
}
}
And I have the following codes in onActivityResult to show that the picture has been taken, so I can proceed the next step:
} else if (requestCode == TAKE_PICTURE) {
开发者_如何学Python if (data == null) {
Toast toast = Toast.makeText(getApplicationContext(),
"Take Picture finished", 10);
toast.show();
}
And I have defined the following settings in AndroidManifest: android.permission.CAMERA and android.permission.WRITE_EXTERNAL_STORAGE
The launch Camera intent works, but when I make a picture and click on Save, then it does not return to onActivityResult and my app crashes.
Can someone help me with this?
Thanks
I got some problems with Galaxy S version 2.3.4 and camera.
After a little work, it actually work with this code (tested with Galaxy S and Nexus S). You can try it and say me if it works for you.
private Uri mCapturedImageURI;
private void takePhoto() {
String fileName = "temp.jpg";
if (isGalaxyS()) {
fileName = Repertoires_S.getInstance().Get_Photos_Path() + fileName;
File fileTmp = new File(fileName);
if (fileTmp.exists()) fileTmp.delete();
mCapturedImageURI = Uri.fromFile(fileTmp);
} else {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION, "Image prise via XXX :)");
mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(intent, PICK_FROM_CAMERA);
}
private boolean isGalaxyS() {
Log.d("Photo", android.os.Build.BRAND + "/" + android.os.Build.PRODUCT + "/"
+ android.os.Build.DEVICE);
ArrayList<String> devices = new ArrayList<String>();
devices.add("samsung/GT-I9000/GT-I9000");
return devices.contains(android.os.Build.BRAND + "/" + android.os.Build.PRODUCT + "/"
+ android.os.Build.DEVICE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
switch (requestCode) {
case PICK_FROM_CAMERA: {
Bitmap photo = null;
String filenameDest = Repertoires_S.getInstance().Get_Photos_Path() + DateTime_BO_S.getInstance().Date_Courante_AAAAMMJJ_HHMMSS() + ".jpg";
File fDest = new File(filenameDest);
String capturedImageFilePath;
if (data == null) {
// "intent.putExtra(MediaStore.EXTRA_OUTPUT, XXXX);" utilisé
try {
if (isGalaxyS()) {
String fileName = "temp.jpg";
capturedImageFilePath = Repertoires_S.getInstance().Get_Photos_Path() + fileName;
} else {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
capturedImageFilePath = cursor.getString(column_index_data);
}
File fSrc = new File(capturedImageFilePath);
fSrc.renameTo(fDest);
photo = BitmapFactory.decodeFile(filenameDest);
} catch (Exception e) {
e.printStackTrace();
}
} else {
// "intent.putExtra(MediaStore.EXTRA_OUTPUT, XXXX);" non utilisé -> miniature
Bundle extras = data.getExtras();
if (extras != null) {
photo = extras.getParcelable("data");
}
}
if (photo != null) {
try {
int tailleMaxPhoto = 800;
double rap;
int newWidth;
int newHeight;
if (photo.getWidth() > tailleMaxPhoto || photo.getHeight() > tailleMaxPhoto) {
// Si c'est plus grand on redimensionne
if (photo.getWidth() > photo.getHeight()) {
rap = (double)tailleMaxPhoto / photo.getWidth();
newWidth = tailleMaxPhoto;
newHeight = (int)((double)photo.getHeight() * rap);
} else {
rap = (double)tailleMaxPhoto / photo.getHeight();
newHeight = tailleMaxPhoto;
newWidth = (int)((double)photo.getWidth() * rap);
}
Bitmap photoSmall = Bitmap.createScaledBitmap(photo, newWidth, newHeight, true);
if (photoSmall != null) {
FileOutputStream out = new FileOutputStream(filenameDest);
if (photoSmall.compress(Bitmap.CompressFormat.JPEG, 90, out)) {
mPhotosPrat.add(filenameDest);
mPhotoAdapt.notifyDataSetChanged();
}
}
} else {
mPhotosPrat.add(filenameDest);
mPhotoAdapt.notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
}
}
break;
}
}
}
精彩评论