问题
Using code from this answer: https://stackoverflow.com/a/12347567/734687
I use the following code to let a user submit a picture to my Android application.
private void openImageIntent() {
File outputFile = null;
try {
outputFile = File.createTempFile("tmp", "face", getCacheDir());
} catch (IOException pE) {
pE.printStackTrace();
}
mOutputFileUri = Uri.fromFile(outputFile);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mOutputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_PICK);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, 42); //XXX 42?
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 42) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri = null;
if (isCamera) {
selectedImageUri = mOutputFileUri;
} else {
selectedImageUri = data == null ? null : data.getData();
}
loadFacePicture(selectedImageUri);
}
}
}
However the Uri selectedImageUri
leads to an empty file on loadFacePicture(selectedImageUri);
What did I missed ?
回答1:
This is what I endup doing. This seemed to be the source of the issue:
intent.putExtra(MediaStore.EXTRA_OUTPUT, mOutputFileUri);
The actual code:
private void openImageIntent() {
try {
outputFile = File.createTempFile("tmp", ".jpg", getCacheDir());
} catch (IOException pE) {
pE.printStackTrace();
}
mOutputFileUri = Uri.fromFile(outputFile);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_PICK);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, 42);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 42) {
Bitmap bmp = null;
if (data.hasExtra("data")) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
} else {
AssetFileDescriptor fd = null;
try {
fd = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
} catch (FileNotFoundException pE) {
pE.printStackTrace();
}
bmp = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor());
}
try {
FileOutputStream out = new FileOutputStream(new File(mOutputFileUri.getPath()));
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
loadFacePicture(mOutputFileUri);
}
}
}
来源:https://stackoverflow.com/questions/38090474/image-file-is-empty-on-return-from-activity-on-android