将捕获的图像从相机设置为ImageButton背景

时间:2012-12-15 10:24:42

标签: android imagebutton

我正在从相机拍摄照片,并希望将其设置为我的ImageButton背景的背景。

拍摄照片,将其呈现在ImageView中并将其保存到设备中,并且完整路径效果很好。

如果这很重要,这是我在OnActivityResult中的处理代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { // Handling the photo

    ImageView imageView=(ImageView)findViewById(R.id.imageView1);
    if (requestCode == TAKE_A_PHOTO) {
        // Check if the result includes a thumbnail Bitmap
        if (data != null) {
            if (data.hasExtra("data")) {
                Bitmap thumbnail=data.getParcelableExtra("data");
                imageView.setImageBitmap(thumbnail);
            }
        } else {
            // If there is no thumbnail image data, the image
            // will have been stored in the target output URI.
            // Resize the full image to fit in out image view.
            int width = imageView.getWidth();
            int height = imageView.getHeight();
            BitmapFactory.Options factoryOptions = new BitmapFactory.Options();
            factoryOptions.inJustDecodeBounds = true; BitmapFactory.decodeFile(outputFileUri.getPath(),
                    factoryOptions);
            int imageWidth = factoryOptions.outWidth; int imageHeight = factoryOptions.outHeight;
            // Determine how much to scale down the image
            int scaleFactor = Math.min(imageWidth/width, imageHeight/height);
            // Decode the image file into a Bitmap sized to fill the View
            factoryOptions.inJustDecodeBounds = false; factoryOptions.inSampleSize = scaleFactor; factoryOptions.inPurgeable = true;
            Bitmap bitmap = BitmapFactory.decodeFile(outputFileUri.getPath(),
                    factoryOptions);
            imageView.setImageBitmap(bitmap);
            Toast.makeText(this, "Image saved to "+ incomePhotoURL, Toast.LENGTH_LONG).show();

        }
    }
    else if (requestCode == LOAD_AN_IMAGE) {
        Uri selectedImageUri=data.getData();
        isAudioRecordPath=idanUtils.getRealPathFromURI(selectedImageUri, AddIncome.this);
        Toast.makeText(this, "Image loaded from "+incomePhotoURL, Toast.LENGTH_LONG).show();
        Log.i(ItemsDataBase.TAG, "Image loaded from "+incomePhotoURL);
    }
}

0 个答案:

没有答案