Get image ByteArray from onActivityResult to oncreate

I have global variable byte[] byteArr; which i want to save value of bytearray when image is clicked and set the value after TakeImage(img_view); method in ed[finalI].setText(bytearray); This below code is above onCreate

private Bitmap global_bitmap;

This below code is in onCreate from which when i click it popups the option to select gallery

final XEditText ed[] = new XEditText[arraylist.size()];
final XImageView xImageViews[] = new XImageView[arraylist.size()];



    for (int i = 0; i < arraylist.size(); i++) {
  ed[i] = new XEditText(this);
  xImageViews[i] = new XImageView(this);

ed[i].setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

TakeImage(img_view);
xImageViews[finalI1].setImageBitmap(global_bitmap);
ed[finalI1].setText(global_bitmap.toString());

}
  });


}

This the method i am calling from oncreate

public void TakeImage(final ImageView imageview) {

        AlertDialog.Builder builderimage = new AlertDialog.Builder(JSON_Form_Activity.this);
        builderimage.setTitle("Choose Image");
        builderimage.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
            }
        });



        builderimage.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });


        builderimage.show();

    }

After this there are methods for onActivityResult like below

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
            Uri uri = data.getData();
            try {
                img_view.setVisibility(View.VISIBLE);
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byteArr = stream.toByteArray();

                Path_Image_model path_image_model = new Path_Image_model();
                path_image_model.setPath(byteArr.toString());

                Log.d(TAG, "===byteArr_PickImage==" + byteArr);
                img_view.setImageBitmap(bitmap);
                  global_bitmap=bitmap;


                resizedFile = new File(uri.getPath()); 
                Log.d(TAG, "===FileImage==" + resizedFile);


            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    public Uri getImageUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    }

    public String getRealPathFromURI(Uri uri) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(idx);
    }   

On the first go it shows me null value and on second go its shows one previous value i want real bytearray value of selected imageview from Gallery on every img_btn.setOnClickListener click

1 answer

  • answered 2018-10-23 11:39 Asad Ali

    onActivityResultMethod:

            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
                    Uri uri = data.getData();
                    try {
                        img_view.setVisibility(View.VISIBLE);
                        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                        byteArr = stream.toByteArray();
    
                        Path_Image_model path_image_model = new Path_Image_model();
                        path_image_model.setPath(byteArr.toString());
    
                        Log.d(TAG, "===byteArr_PickImage==" + byteArr);
                        img_view.setImageBitmap(bitmap);
    
                        ed[finalI].setText(byteArr);
    
                        resizedFile = new File(uri.getPath()); 
                        Log.d(TAG, "===FileImage==" + resizedFile);
    
    
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                }
    

    Please update you onActivityResultMethod