Abin Thaha
Abin Thaha

Reputation: 4643

How to convert an Image to Bits array in android

I am new to android development, Now currently am working on an App for Image Steganography. So in my app , I need to convert an Image that i selected from gallery to Bits array(Each pixel will have a 8 bit value, thats what i mean), How can i do it ? Can anybody help me ?

public class ImageActivity extends Activity {
private Button btnSelectImage;
private Button btnEncode;
String Pathfile=new String();
public String selectedImagePath;
private ImageView myImage;
Bitmap result;
public static final int ICON_SELECT_GALLERY = 1;
private static final Object IMAGE_TAKER_REQUEST = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);

    btnSelectImage = (Button) findViewById(R.id.button1);
    btnEncode = (Button) findViewById(R.id.button2);
    btnSelectImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            selectImage();
        }
    });

    myImage = (ImageView) findViewById(R.id.imageView1);

}
    public void selectImage() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    startActivityForResult(intent, ICON_SELECT_GALLERY);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if (resultCode == RESULT_OK) 
    {
        if (requestCode == 1) 
        {
            Uri selectedImageUri = data.getData();
            String selectedImagePath = getPath(selectedImageUri);
            Log.v("IMAGE PATH====>>>> ",selectedImagePath);

            TextView imgPath=(TextView)findViewById(R.id.textView2);
            imgPath.setText(selectedImagePath);
            Pathfile=new String(selectedImagePath);

            result = BitmapFactory.decodeFile(Pathfile);
            myImage.setImageBitmap(result);
        }
    }
}

public String getPath(Uri uri) 
{
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

}

Upvotes: 0

Views: 830

Answers (1)

Parag Chauhan
Parag Chauhan

Reputation: 35986

Pass Bitmap and method will return byte[]

public static byte[] getBytesFromBitmap(Bitmap bitmap){
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 70, stream);
    return stream.toByteArray();
}

Upvotes: 1

Related Questions