1

i want to convert image to byte array.

my code is:

private Uri mImageCaptureUri;
private ImageView mImageView;

private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_FILE = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_picker_activty);
    final String[] items = new String[]{"From Camera", "From SD Card"};
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.select_dialog_item, items);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Select Image");
    builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            if (item == 0) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File file = new File(Environment.getExternalStorageDirectory(),
                        "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
                mImageCaptureUri = Uri.fromFile(file);

                try {
                    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
                    intent.putExtra("return-data", true);

                    startActivityForResult(intent, PICK_FROM_CAMERA);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                dialog.cancel();
            } else {
                Intent intent = new Intent();

                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);

                startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
            }
        }
    });

    final AlertDialog dialog = builder.create();

    mImageView = (ImageView) findViewById(R.id.iv_pic);

    (findViewById(R.id.btn_choose)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.show();
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Picasso.with(this).load(data.getData()).into(mImageView);

}

i dont know how do i get image bitmap from this and convert to byte array

i want to store the image byte array in sqlite database. thanks in advance. plzz help me with this

nishant
  • 2,526
  • 1
  • 13
  • 19

2 Answers2

1

Step #1: Delete getRealPathFromURI().

Step #2: Delete the entire current contents of your onActivityResult() method.

Step #3: Use an image-loading library, such as Picasso, to load the image into your ImageView in a revised onActivityResult() method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode==RESULT_OK) {
    Picasso.with(this).load(data.getData()).into(mImageView);
  }
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • what if i want to store the image in my database? – nishant Mar 02 '17 at 15:24
  • @nishant: Picasso also lets you set a callback to get access to the `Bitmap` that it creates. Or, fork your own background thread, which uses `ContentResolver` and `openInputStream()` to get an `InputStream` on the content identified by the `Uri`. Copy that data into a `ByteArrayOutputStream`, and use the resulting `byte[]` in your database I/O. – CommonsWare Mar 02 '17 at 16:01
  • how do i set a callback to get access to the Bitmap? – nishant Mar 02 '17 at 17:07
  • @nishant: http://stackoverflow.com/questions/20181491/use-picasso-to-get-a-callback-with-a-bitmap – CommonsWare Mar 02 '17 at 17:12
0
 mImageView.setImageBitmap(BitmapFactory.decodeStream(
   getContentResolver().openInputStream(data.getData())));

You dont even need a library.

greenapps
  • 11,154
  • 2
  • 16
  • 19