Exception occurred while converting byte [] array to String in android

float rotation =getRotatedImage(imgdata);

      

// Where imgdata is the string required by the getRotatedImage (imgdata) method.

// My own way to get the image routine

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;
    }

      

// internally in the picture I convert byte data to string

@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);

    }

      

// Here's my method to convert to string

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

        return new String(finalimage2);

    }

      

// Here's my details about the logcat exception:

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                 

      

// so please someone tell me what to do, also tried Base64 of android class

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

      

where imgdata is a string, finalimage is an array of bytes.

Waiting for an answer. Please forgive me for my language and coding style.

// Here I mention the button click event for which an exception is thrown

@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();

      

an exception is thrown when this button is clicked. // inside resizeimagefroshow () calls getRotatedimage (), after which a new bitmap image is created to display

// code here:

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);

      

+3


source to share


1 answer


As @ njzk2 mentioned, you are converting binary image data to string, it doesn't make any sense. What you want is a filename string that is passed to the constructor ExifInterface

.

So first you have to save the image data to a file:

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

      



Then you can use ExifInterface

with filename

.

If you want to read the metadata directly from the stream, you will have to use a third party library. See reading EXIF โ€‹โ€‹jpeg EXIF โ€‹โ€‹metadata from image callback .

0


source







All Articles