0

I am trying to create a camera intent and a sub portion of the code is given below.

public void onPictureTaken(byte[] data, Camera camera) {
    String dat = new String(data);          
    byte[] datas = dat.getBytes();                  
    preview.setVisibility(View.GONE);
    ImageView iv2 = (ImageView)findViewById(R.id.iv1);
    Bitmap bMap = BitmapFactory.decodeByteArray(datas, 0, datas.length);
    iv2.setImageBitmap(bMap);           
}

This keeps the imageview blank, however when I give

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

the imageview is properly loaded. Am I doing any mistake in byte array to string conversion??

Renjith
  • 5,783
  • 9
  • 31
  • 42
bharath
  • 953
  • 4
  • 17
  • 30

2 Answers2

0

why do you need these two lines?

String dat = new String(data);          
byte[] datas = dat.getBytes(); 

use data directly in the decodeByteArray

stinepike
  • 54,068
  • 14
  • 92
  • 112
  • I want to pass this in an intent and receive it in onactivity result for the camera activity, the conversion to string is needed to pass and reconversion to byte array to receive, to test it, i am doing this in the same application – bharath Apr 15 '13 at 05:57
  • http://stackoverflow.com/questions/1536054/how-to-convert-byte-array-to-string-and-vice-versa .. look at this question .. it will solve your querry i guess – stinepike Apr 15 '13 at 06:01
  • 1
    Take a look at this....[http://stackoverflow.com/questions/7947871/convert-a-string-to-a-byte-array-and-then-back-to-the-original-string] – AndiM Apr 15 '13 at 06:15
0

You dont even need to convert the byte[] to String. Just use it as it is.

public void onPictureTaken(byte[] data, Camera camera) {
    preview.setVisibility(View.GONE);
    ImageView iv2 = (ImageView)findViewById(R.id.iv1);

    // ensure ImageView is visible.
    iv2.setVisibility( View.VISIBLE);

    Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length);
    iv2.setImageBitmap(bMap);           
}

Just use the above modified code.

JaydeepW
  • 3,237
  • 1
  • 25
  • 27