Load Photograph As Well As Scale-Down To Adapt Imageview

Posted on

This representative exhibit how to charge photograph using Intent.ACTION_OPEN_DOCUMENT, in addition to scale-down to suitable size, to display inward ImageView.


MainActivity.java

package com.blogspot.android_er.androidimage;  import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast;  import java.io.FileNotFoundException;  populace degree MainActivity extends AppCompatActivity {      soul static lastly int RQS_OPEN = 1;     Button buttonOpen;     ImageView imageView;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         buttonOpen = (Button) findViewById(R.id.opendocument);         buttonOpen.setOnClickListener(buttonOpenOnClickListener);          imageView = (ImageView)findViewById(R.id.image);     }      View.OnClickListener buttonOpenOnClickListener =             novel View.OnClickListener() {                 @Override                 populace void onClick(View v) {                     Intent intent = novel Intent();                     intent.setAction(Intent.ACTION_OPEN_DOCUMENT);                     intent.addCategory(Intent.CATEGORY_OPENABLE);                     intent.setType("image/*");                     startActivityForResult(intent, RQS_OPEN);                 }             };      @Override     protected void onActivityResult(int requestCode, int resultCode, Intent data) {         if (resultCode == RESULT_OK && requestCode == RQS_OPEN) {             Uri dataUri = data.getData();             int w = imageView.getWidth();             int h = imageView.getHeight();             Toast.makeText(MainActivity.this,                     dataUri.toString() + "\n" +                     w + " : " + h,                     Toast.LENGTH_LONG).show();              drive {                 Bitmap bm = loadScaledBitmap(dataUri, w, h);                 imageView.setImageBitmap(bm);                 Toast.makeText(MainActivity.this,                         bm.getWidth() + " x " + bm.getHeight(),                         Toast.LENGTH_SHORT).show();             } grab (FileNotFoundException e) {                 e.printStackTrace();                 Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();             }         }     }      /*     reference:     Load scaled bitmap            */     soul Bitmap loadScaledBitmap(Uri src, int req_w, int req_h) throws FileNotFoundException {          Bitmap bm = null;          // First decode amongst inJustDecodeBounds=true to cheque dimensions         lastly BitmapFactory.Options options = novel BitmapFactory.Options();         options.inJustDecodeBounds = true;         BitmapFactory.decodeStream(getBaseContext().getContentResolver().openInputStream(src),                 null, options);          // Calculate inSampleSize         options.inSampleSize = calculateInSampleSize(options, req_w, req_h);          // Decode bitmap amongst inSampleSize laid upward         options.inJustDecodeBounds = false;         bm = BitmapFactory.decodeStream(                 getBaseContext().getContentResolver().openInputStream(src), null, options);          provide bm;     }      populace int calculateInSampleSize(BitmapFactory.Options options,                                      int reqWidth, int reqHeight) {         // Raw elevation in addition to width of ikon         lastly int elevation = options.outHeight;         lastly int width = options.outWidth;         int inSampleSize = 1;          if (height > reqHeight || width > reqWidth) {              // Calculate ratios of elevation in addition to width to requested elevation in addition to             // width             lastly int heightRatio = Math.round((float) elevation                     / (float) reqHeight);             lastly int widthRatio = Math.round((float) width / (float) reqWidth);              // Choose the smallest ratio every 2d inSampleSize value, this volition             // guarantee             // a lastly ikon amongst both dimensions larger than or equal to the             // requested elevation in addition to width.             inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;         }          provide inSampleSize;     } }