I need help in improving my code .
What I am doing : There is a button in main activity, when clicked , user choose the image , after that, the image is passed through an intent to another activity(add_image.java) and displayed in an image view , after that I send the image to the server.
My problems:1) I want the best way to send the path image to second intent then convert it into image
2) then compress it as much as I can without loosing a lot of its quality.
the image size now is 376kb . so in my my app Ill displaying several images so in such size it will consume time and internet to load.( I am using picasso and fit() didnt decrease the size.)
here is my code :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
//file name
filePath = data.getData();
try {
// Bundle extras2 = data.getExtras();
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte imageInByte[] = stream.toByteArray();
Intent i = new Intent(this, AddImage.class);
i.putExtra("image", imageInByte);
startActivity(i);
} catch (IOException e) {
e.printStackTrace(); } } }
And here I am receiving the image
byte[] byteArray = getIntent().getByteArrayExtra("image");
encodedImage = Base64.encodeToString(byteArray, Base64);
bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView imageview = (ImageView) findViewById(R.id.imageView);
imageview.setImageBitmap(bmp);
Try following code, you might need to modify some of the parameters as per your requirement :
Create MainActivity as following :
Now create activity_main.xml as follows :
Then we need to create AddImage activity as follows :