无法将图像添加到摄像头捕获的imageview

时间:2014-12-15 07:40:11

标签: android

我正在开发一个应用程序,我必须从相机中捕获图像并添加到ImageView。在ImageView上显示图片时,我遇到了问题。如果我点击保存按钮,图片第一次没有显示在ImageView上,但第二次显示,请解决我的问题,我无法找到解决方法。

代码段:

        fromCamera.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            /*  Log.e("OPEN", "CAMERA");

                 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                 File f = new File(android.os.Environment
                          .getExternalStorageDirectory(), "temp.jpg");
                 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                 startActivityForResult(intent, RESUL_CameraT_LOAD_IMAGE);*/
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                File file = new File(Environment.getExternalStorageDirectory()+File.separator +    
             "fav.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(intent, RESUL_CameraT_LOAD_IMAGE);
                uploadalertDialog.dismiss();
            }
        });


    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Uri selectedImage = null;
    Bitmap bitmap;
    try {
        switch (requestCode) {
        case RESUL_CameraT_LOAD_IMAGE:
            if (resultCode == Activity.RESULT_OK) {
                // imageView.setImageResource(android.R.color.transparent);
                Log.e("GET IMAGE", "PATH");
                try{
                   File file = new File(Environment.getExternalStorageDirectory()+File.separator 
               + "fav.jpg");
                    bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 300, 300);
                    uloadImgView.setImageBitmap(bitmap);
                    ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 90,
                            byteArray);

                    byte[] byte_arr = byteArray.toByteArray();
                    base64 = Base64.encodeToString(byte_arr, Base64.DEFAULT);
                } 
                catch(Exception e){
                    e.printStackTrace();
                }
         }





public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) 
{ // BEST QUALITY MATCH

    //First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize, Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    int inSampleSize = 1;

    if (height > reqHeight) 
    {
        inSampleSize = Math.round((float)height / (float)reqHeight);
    }
    int expectedWidth = width / inSampleSize;

    if (expectedWidth > reqWidth) 
    {
        //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
        inSampleSize = Math.round((float)width / (float)reqWidth);
    }

    options.inSampleSize = inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(path, options);
}

1 个答案:

答案 0 :(得分:0)

首先在应用程序的清单文件中添加以下权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

然后检查我在我的应用程序中使用的以下代码来设置用户的个人资料照片。

//在单击侦听器上搜索相机触发器     image.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            Intent cameraintent = new Intent(
                    MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraintent, 101);

        }
    });

// onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri selectedImage = null;
    Bitmap bitmap;

    try {
        switch (requestCode) {
        case 101:
            if (resultCode == Activity.RESULT_OK) {
                if (null != data) {
                    selectedImage = data.getData(); // the uri of the image
                                                    // taken
                    bitmap = decodeSampledBitmapFromUri(this,
                            selectedImage, 100, 100);
                    image.setImageBitmap(bitmap);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    super.onActivityResult(requestCode, resultCode, data);
}

//位图采样

public static Bitmap decodeSampledBitmapFromUri(Activity callingActivity,
        Uri uri, int reqWidth, int reqHeight) {
    try {
        InputStream input = callingActivity.getContentResolver()
                .openInputStream(uri);
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inSampleSize = 2; // make the bitmap size half of the
                                    // original one
        BitmapFactory.decodeStream(input, null, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);
        input.close();

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        input = callingActivity.getContentResolver().openInputStream(uri);
        Bitmap bitmap = BitmapFactory.decodeStream(input, null, options);
        return bitmap;
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    } catch (IOException e) {// TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

}

//计算样本量

public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and
        // keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}