I'm using Activity Result API to get multiple contents from gallery (since startActivityForResult() is deprecated).
MainActivity.java
LinearLayout ll;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
ll = (Button) findViewById(R.id.ll);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
launchImage.launch("image/*");
}
});
}
ActivityResultLauncher<String> launchImage= registerForActivityResult(
new ActivityResultContracts.GetMultipleContents(),
new ActivityResultCallback<List<Uri>>() {
@Override
public void onActivityResult(List<Uri> uri) {
try {
for (int i = 0; i < uri.size(); i++) {
ImageView imageView = new ImageView(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(600,800);
lp.setMargins(0, 50, 0, 0);
imageView.setLayoutParams(lp);
imageView.setImageURI(uri.get(i));
ll.addView(imageView);
}
}
catch (NullPointerException e) {
e.printStackTrace();
}
}
});
The problem is when too many images are being added, my app crashes saying OutOfMemoryError.
I believe the problem is with the file size of those images.
How do I reduce image size on this ActivityResult API?
Any help would be appreciated.
Thanks in advance.
Error from LogCat: java.lang.OutOfMemoryError: Failed to allocate a 38937612 byte allocation with 16772728 free bytes and 19MB until OOM