将byte []数组转换为String时,在android中获得了异常

时间:2013-01-22 12:51:25

标签: android

float rotation =getRotatedImage(imgdata);

//其中imgdata是方法getRotatedImage(imgdata)上需要的String。

//我自己获取图片的方法

private float getRotatedImage(String imgdata2) {
         try {
            ExifInterface exif = new ExifInterface(imgdata2);
            int rotation = (int)exifOrientationToDegrees(
                    exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL));
            return rotation;
        } catch (IOException e) {
            Log.e("CAMERA IMAGE", "Error checking exif", e);
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // TODO Auto-generated method stub
        return 0f;
    }

//在拍照的内部我将字节数据转换为字符串

@Override
    public void onPictureTaken(byte[] arg0, Camera arg1) {
        Log.i("CAMERA", "ON PICTURE TAKEN");
        int length = arg0.length;
        if (length != 0) {

            //bitimage = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);

            finalimage = arg0;
            if(finalimage!=null)
            {
                imgdata=converttostring(finalimage);
            }
            //compressimageforshow();
            Toast.makeText(getApplicationContext(), "IMAGE PROCESSING DONE", 0)
                    .show();

        } else {
            Toast.makeText(getApplicationContext(), "NO IMAGE ", 0).show();
        }
        arg1.startPreview();
        //shutterButton.setEnabled(false);
        show_image_bt.setEnabled(true);

    }

//这是我的方法,以便转换为字符串

private String converttostring(byte[] finalimage2) {
        // String basestr=Base64.encodeToString(finalimage2, Base64.NO_WRAP);

        return new String(finalimage2);

    }

//这是我的logcat异常详情:

01-22 18:06:48.808: I/ACTIVITY(344): ON CREATE
01-22 18:06:48.808: I/ACTIVITY(344): ON RESUME
01-22 18:06:49.348: I/CAMERA(344): ON SURFACE CHANGE
01-22 18:06:53.018: I/CAMERA(344): ON PICTURE TAKEN
01-22 18:06:56.188: E/(344): can't open '������JFIF����`��`��������fExif����II*������������������>��������������F������(��������������1��������N��������������`������������`������������Paint.NET v3.36������C��
01-22 18:06:56.188: E/(344): 
01-22 18:06:56.188: E/(344): 
01-22 18:06:56.188: E/(344): 

����C       

�������@"�������������������������� 
01-22 18:06:56.188: E/(344): �����������}��!1AQa"q2���#B��R��$3br�  
01-22 18:06:56.188: E/(344): %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������������������������������������������������������������������������������������������   
01-22 18:06:56.188: E/(344): ���������w��!1AQaq"2�B���� #3R�br�
01-22 18:06:56.188: E/(344): $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�����������������

//所以请任何人告诉我该怎么做也尝试了Android类的Base64

imgdata=Base64.encodeToString(finalimage,Base64.NO_WRAP);

其中imgdata是字符串,finalimage是字节数组。

等待回复。请原谅我的语言和编码风格。

//这里我提到了生成异常的按钮点击事件

@Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
case R.id.show_image:
            show_image_imgvw.setVisibility(View.VISIBLE);
            retake_photo_bt.setEnabled(true);
            show_image_bt.setEnabled(false);
            preview.setVisibility(View.INVISIBLE);

            if (finalimage != null) {
                resizeimageforshow();
                show_image_imgvw.setImageBitmap(bitimage);
                Toast.makeText(getApplicationContext(), "IMAGE DISPLAYED", 0)
                        .show();

单击此按钮异常即生成。 //调用getRotatedimage()的resizeimagefroshow()内部,然后创建新的位图 用于显示的图像

//代码在这里:

private void resizeimageforshow() {
        if (finalimage != null) {
            // bitimage=BitmapFactory.decodeByteArray(finalimage, 0,
            // finalimage.length);
            // show_image_imgvw.setImageBitmap(bitimage);
            Bitmap myimage1 = BitmapFactory.decodeByteArray(finalimage, 0,
                    finalimage.length);
            // imgdata=Base64.encodeToString(finalimage,Base64.NO_WRAP);
            android.graphics.Matrix matrix = new android.graphics.Matrix();
            float rotation = getRotatedImage(imgdata);
            if (rotation != 0f) {
                matrix.preRotate(rotation);
            }
            bitimage = Bitmap.createBitmap(myimage1, 0, 0, myimage1.getWidth(),
                    myimage1.getHeight(), matrix, true);

1 个答案:

答案 0 :(得分:0)

正如@ n​​jzk2所提到的那样,您将二进制图像数据转换为字符串没有任何意义。你想要的是一个传递给ExifInterface构造函数的文件名字符串。

首先,您必须将图像数据保存到文件中:

File file = new File(getExternalCacheDir(), "camera_picture.jpg");
OutputStream os = new FileOutputStream(file.toString());
try {
    os.write(finalimage);
} finally {
    os.close();
}

然后,您可以将ExifInterfacefilename一起使用。

如果您想直接从流中读取元数据,则必须使用第三方库。请参阅reading android jpeg EXIF metadata from picture callback

相关问题