用相机拍摄照片,但图像大小为0 KB

时间:2011-09-10 00:22:09

标签: android camera android-camera

我做了一个演示,用凸轮制作照片,保存图像,然后,在其他活动中显示最后一张照片。这对于模拟器来说没问题,但是当我在真实手机中安装我的演示时,我可以制作图片,但保存的文件大小为O KB。

//This is the method where I make the photo
    private boolean makePhoto(){
        try{
            ImageCaptureCallback camDemo = null;
            SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
            String filenameTimeStamp = timeStampFormat.format(new Date());
            ContentValues values = new ContentValues();
            values.put(MediaColumns.TITLE, String.format("%s.jpg", filenameTimeStamp));
            values.put(ImageColumns.DESCRIPTION, "Imagen desde Android Emulator");
            Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);

            Log.d("titulo: ", values.get(MediaColumns.TITLE).toString());
            camDemo = new ImageCaptureCallback(getContentResolver().openOutputStream(uri)); 
            this.camera.takePicture(this.mShutterCallback, this.mPictureCallback, camDemo);
            Log.d("makePhoto", "Foto hecha");
            return true;
        }catch(Exception ex){
            ex.printStackTrace();
            Context context = getApplicationContext();          
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, ex.toString(), duration);
            toast.show();
        }       
        return false;
    }

    //This is the object where the pic taken is saved
public class ImageCaptureCallback implements PictureCallback {
    private OutputStream fileoutputStream;

    public ImageCaptureCallback(OutputStream fileoutputStream){
        this.fileoutputStream = fileoutputStream;
    }

    public void onPictureTaken(byte[] data, Camera camera){
        try{
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize = 1;

            Bitmap myImage = BitmapFactory.decodeByteArray(data, 0, data.length,options);


            BufferedOutputStream bos = new BufferedOutputStream(this.fileoutputStream);

            myImage.compress(CompressFormat.JPEG, 75, bos);

            bos.flush();
            bos.close();

        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

}

发生了什么事?

1 个答案:

答案 0 :(得分:0)

我正在向您发送拍照的代码并将照片带入您的应用程序。

首先添加以下内容:

File imageFile = new File(imageFilePath);
Uri imageFileUri = Uri.fromFile(imageFile);
Intent i = new Intent(
    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(i, CAMERA_PIC_REQUEST);

然后在代码中添加以下startActivityForResule

protected void onActivityResult(int requestCode, int resultCode,
            Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
        switch (requestCode) 
{
case SELECT_PICTURE:

Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
// file path of selected image
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
encode = Base64.encodeBytes(byteArray);
try {
    byte[] decode = Base64.decode(encode);
    Bitmap bmp = BitmapFactory.decodeByteArray(decode, 0,
            decode.length);
    imgview_photo.setImageBitmap(bmp);
    btn_getphoto.setVisibility(View.INVISIBLE);
    btn_cancel.setVisibility(View.VISIBLE);
    btn_upload.setVisibility(View.VISIBLE);
    } 
catch (IOException e) {
    e.printStackTrace();
}
break;
case CAMERA_PIC_REQUEST:
    Bitmap bmp = BitmapFactory.decodeFile(imageFilePath);
    int width = bmp.getWidth();
    int height = bmp.getHeight();
    float scaleWidth = ((float) 300) / width;
    float scaleHeight = ((float) 300) / height;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width,
        height, matrix, true);
    ByteArrayOutputStream baostream = new ByteArrayOutputStream();
    resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, baostream);
    byte[] byteArrays = baostream.toByteArray();
    encode = Base64.encodeBytes(byteArrays);
    try {
        byte[] decode = Base64.decode(encode);
        Bitmap bitmp = BitmapFactory.decodeByteArray(decode, 0,
                decode.length);
        imgview_photo.setImageBitmap(bitmp);
        btn_getphoto.setVisibility(View.INVISIBLE);
        btn_cancel.setVisibility(View.VISIBLE);
        btn_upload.setVisibility(View.VISIBLE);
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
}