sudad najim
sudad najim

Reputation: 5

How to get bitmap information from onActivityResult and use it in the same activity

I get bitmap image from gallery using protected void onActivityResult . Now I want to get the information of this bitmap image to use it in another method in the same activity . This my code: I get bitmap image from gallery using protected void onActivityResult . Now I want to get the information of this bitmap image to use it in another method in the same activity . This my code:

public class MainActivity extends AppCompatActivity {
private static final int selected_image = 1;
Bitmap bitmap;
ImageView imagev;
public static int n, m;

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

    imagev = findViewById(R.id.imagev);
}


public void btnselectimage(View view) {
    Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, selected_image);
    // I want get the width and height of this image here;
    System.out.println("width=" + n);
    System.out.println("height=" + m);
}


//  @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case selected_image:
            if (resultCode == RESULT_OK) {

                Uri uri = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                bitmap = BitmapFactory.decodeFile(picturePath);
                n = bitmap.getWidth();
                m = bitmap.getHeight();
                //  Drawable d = new BitmapDrawable(bitmap);
                imagev.setImageBitmap(bitmap);
            }
            break;
    }
  }
}

Upvotes: 0

Views: 641

Answers (2)

3iL
3iL

Reputation: 2176

Once you set bitmap to image, You can get the bitmap attached to the same ImageView in different method like this:

Bitmap bitmap = ((BitmapDrawable)imagev.getDrawable()).getBitmap();

Upvotes: 1

Mayur
Mayur

Reputation: 71

    Bitmap bitmap;
    if (mImageViewer.getDrawable() instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) mImageViewer.getDrawable()).getBitmap();
    } else {
       imageView.invalidate();
       BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
       bitmap = drawable.getBitmap();
    }

Upvotes: 3

Related Questions