Android -更新文件夹显示后捕获图像- 2图像显示



图库出现问题。我正在做图像捕获功能使用:

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;

您可能正在存储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卡)中。此代码处理临时文件并更新图库。

最新更新