在Android中,相机预览错误和缩略图可以

时间:2015-12-07 11:49:40

标签: android uiimageview android-camera android-external-storage

我正在使用imageview将捕获的图像保留在布局中并保存到存储空间中。除了一个,我的代码表现都很好。在捕获图像之后,图像视图具有我捕获的最后一个最新图像,并且在存储器中还具有显示最后捕获图像的缩略图。

但是当我点击拇指看预览时,它总是显示为第一张图像。在保存图像的同时,我一直保持同名。如何解决此问题以查看最新保存图像的预览。我不想改名。

     @Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v.getId()==R.id.cust_save)
        saveCustomerData();
    if(v.getId()==R.id.cust_photo)
        mobile11=mobile1.getText().toString();
        captureImage();
}
private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);     
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);   
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
private void saveCustomerData() {
    // TODO Auto-generated method stub

}
/**
 * Here we store the file url as it will be null after returning from camera
 * app
 */
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    // save file url in bundle as it will be null on scren orientation
    // changes
    outState.putParcelable("file_uri", fileUri);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    // get the file url
    fileUri = savedInstanceState.getParcelable("file_uri");
}

/**
 * Receiving activity result method will be called after closing the camera
 * */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if the result is capturing Image
    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // successfully captured the image
            // display it in image view
            previewCapturedImage();
        } else if (resultCode == RESULT_CANCELED) {
            // user cancelled Image capture
            Toast.makeText(getApplicationContext(),
                    "User cancelled image capture", Toast.LENGTH_SHORT)
                    .show();
        } else {
            // failed to capture image
            Toast.makeText(getApplicationContext(),
                    "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

/**
 * Display image from a path to ImageView
 */
private void previewCapturedImage() {
    try {
        //cam_img.setVisibility(View.VISIBLE);

        // bimatp factory
        BitmapFactory.Options options = new BitmapFactory.Options();

        // downsizing image as it throws OutOfMemory Exception for larger
        // images
        options.inSampleSize = 8;

        final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                options);
        Log.e("File Storage is : ",fileUri.getPath());

        cam_img.setImageBitmap(bitmap);
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

/**
 * ------------ Helper Methods ---------------------- 
 * */

/**
 * Creating file uri to store image/video
 */
public Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

/**
 * returning image / video
 */
private static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            IMAGE_DIRECTORY_NAME);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                    + IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }

    // Create a media file name
    //String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            //Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() +File.separator + mobile11 +".jpg");
    } 
    else {
        return null;
    }

    return mediaFile;
}

0 个答案:

没有答案