In AddMoreClaims activity has an imageView , button and saves a button .
When the button is pressed, it will go to activeGallery() and allow the user to select an image from the gallery. The selected image will be displayed on imageView AddMoreClaims .
private void activeGallery() { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, RESULT_LOAD_IMAGE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case RESULT_LOAD_IMAGE: if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) { Uri selectedImage = data.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver() .query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); cursor.close(); photo = decodeSampledBitmapFromUri(picturePath,200,200); // make image clear imageView.setImageBitmap(photo); // display image on imageView } break; } public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) { Bitmap bm = null; // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; bm = BitmapFactory.decodeFile(path, options); return bm; } public int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 // and keeps both height and width larger than the requested // height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } }
To make sure the image selected on imageView is clear, I added decodeSampledBitmapFromUri(picturePath,200,200) . So far, everything is working fine.
When you click the save button, the AddClaims listView image is returned .
saveBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent returnIntent = new Intent();
However, the above coding does not work. I'm not sure, because the image is too large, when I click the save button and want to return the image to the listView of AddClaims , it just goes back to activity before AddClaims. . But the code works for the selected image. Why is this going to happen?