有库的问题。我正在使用以下方式进行图像捕捉功能:
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
我用
刷新活动Intent myIntent = getIntent();
finish();
startActivity(myIntent);
画廊确实在巨大的差距之后得到了更新。同样在1点击它捕获2张图片!!我希望只拍摄一次图像!! ..请帮忙!!!
代码:
final ImageButton captureBtn = (ImageButton) findViewById(R.id.captureBtn);
captureBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String fileName = "IMG_" + sdf.format(new Date()) + ".png";
File myDirectory = new File(Environment
.getExternalStorageDirectory() + "/DCIM/Camera");
if(!myDirectory.exists()){
myDirectory.mkdir();
}
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(myDirectory, fileName);
imageUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_IMAGE);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case TAKE_IMAGE:
try {
if (resultCode == RESULT_OK) {
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, imageUri));
mScanner = new MediaScannerConnection(CameraGalleryActivity.this,new MediaScannerConnection.MediaScannerConnectionClient() {
public void onMediaScannerConnected() {
mScanner.scanFile(Environment.getExternalStorageDirectory()+ "/DCIM/Camera", null /* mimeType */);
}
public void onScanCompleted(String path, Uri uri) {
Intent myIntent = getIntent();
finish();
startActivity(myIntent);
mScanner.disconnect();
}
});
mScanner.connect();
}
} catch (Exception e) {
e.printStackTrace();
}
break;
答案 0 :(得分:0)
您可能正在存储Android在使用相机拍摄照片时创建的临时文件。如果您使用按钮激活相机,可以尝试以下功能:
private void cameraClick(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
tempPhoto = createTemporaryFile("picture", ".jpg");
if (tempPhoto != null) {
tempPhoto.delete();
}
} catch (Exception e){
Toast toast = Toast.makeText(mContext, "Unable to create temporary file.", Toast.LENGTH_SHORT);
toast.show();
}
if (tempPhoto != null) {
imageUri = Uri.fromFile(tempPhoto);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, TAKE_PICTURE);
}
} else {
Toast toast = Toast.makeText(mContext, "Unable to create temporary file.", Toast.LENGTH_SHORT);
toast.show();
}
}
private File createTemporaryFile(String part, String ext) throws Exception {
if (isExternalStorageWritable()) {
File tempDir = Environment.getExternalStorageDirectory();
tempDir = new File(tempDir.getAbsolutePath()+"/.temp/");
if(!tempDir.exists()) {
tempDir.mkdir();
}
return File.createTempFile(part, ext, tempDir);
} else {
return null;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
try {
grabImage();
} catch (Exception ex){
Toast toast = Toast.makeText(this, "Unable to grab image.", Toast.LENGTH_SHORT);
toast.show();
}
}
}
public void grabImage() {
this.getContentResolver().notifyChange(imageUri, null);
ContentResolver cr = this.getContentResolver();
Bitmap bitmap = null;
try {
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, imageUri);
createImageFile(bitmap);
} catch (Exception e) {
Toast toast = Toast.makeText(this, "Unable to create file.", Toast.LENGTH_SHORT);
toast.show();
}
}
private void createImageFile(Bitmap bitmap) throws IOException {
// Convert bitmap to JPEG and store it in a byte array
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
byte[] bitmapdata = bytes.toByteArray();
// Generate file name and directory paths
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new java.util.Date());
String imageFileName = "JPEG_" + timeStamp + ".jpg";
if (isExternalStorageWritable()){
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "City Tour";
// Generate storage directory
File storageDir = new File(path);
if (!storageDir.exists()) {
storageDir.mkdir();
}
// Create file to store the image
File imageFile = new File(path + File.separator + imageFileName);
imageFile.createNewFile();
// Write image data to image file
FileOutputStream output = new FileOutputStream(imageFile);
output.write(bitmapdata);
// Make image file visible from gallery
mCurrentPhotoPath = "file:" + imageFile.getAbsolutePath();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(mCurrentPhotoPath)));
output.close();
} else {
Toast toast = Toast.makeText(this, "Unable to access external storage.", Toast.LENGTH_SHORT);
toast.show();
}
}
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
请注意,在这种情况下,我将文件存储在外部设备(SD卡)中。此代码处理临时文件并更新库。希望这会有所帮助。