Android Take a Photo and put the thumbnail in ImageView, AND save to file

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent intentdata) {
    Log.i("MainActivity", "onActivityResult");
    if (requestCode == REQUEST_IMAGE_CAPTURE ){
        Log.i("MainActivity", "ImageRequest:"+resultCode);
        if( resultCode == RESULT_OK) {

            File file = new File(mCurrentPhotoPath);
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.fromFile(file));

                mImageView.setImageBitmap(crupAndScale(bitmap,300));
            } catch (Exception e) {
                Log.e("MainActivity","Create bitmap exception");
            }



        }
        if( resultCode == RESULT_CANCELED)
        {
            Log.i("MainActivity", "RESULT_CANCELED");
        }
    }
}
public static  Bitmap crupAndScale (Bitmap source,int scale){
    int factor = source.getHeight() <= source.getWidth() ? source.getHeight(): source.getWidth();
    int longer = source.getHeight() >= source.getWidth() ? source.getHeight(): source.getWidth();
    int x = source.getHeight() >= source.getWidth() ?0:(longer-factor)/2;
    int y = source.getHeight() <= source.getWidth() ?0:(longer-factor)/2;
    source = Bitmap.createBitmap(source, x, y, factor, factor);
    source = Bitmap.createScaledBitmap(source, scale, scale, false);
    return source;
}
private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File           Log.e("MainActivity","create photoFile error");
        }
        if (photoFile != null) {
            Uri uri = Uri.fromFile(photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }

        //startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);    }
}
private File createImageFile() throws IOException {
    // Create an image file name    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */            ".jpg",         /* suffix */            storageDir      /* directory */    );

    // Save a file: path for use with ACTION_VIEW intents    mCurrentPhotoPath = image.getAbsolutePath();
    Log.i("MainActivity",mCurrentPhotoPath.toString());
    return image;
}

ความคิดเห็น

บทความที่ได้รับความนิยม