code directly
1 public class MainActivity extends Activity { 2 private EditText ed; 3 private ImageView iv; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 ed = (EditText) findViewById(R.id.ed); 9 iv = (ImageView) findViewById(R.id.iv); 10 } 11 public void see(View view) { 12 // Determine the picture to be loaded (for debugging, put all the pictures in the SD card, then enter the name of the picture on the interface, and splicing the string according to the name) 13 String fileName = ed.getText() .toString(); 14 String path = Environment.getExternalStorageDirectory().getPath()+ "/" + fileName; 15 16 // This class is the inner class of BitmapFactory, used to encapsulate the parameter object 17 BitmapFactory. Options opts = new BitmapFactory.Options(); 18 19 // Do not apply for memory for pixels, only get the width and height information of the picture 20 // The field inJustDecodeBound is set to true, then the bitmap factory returns empty when it builds a BitMap object value, but will return some information of the picture in the Options object (such as the width and height of the picture) 21 opts.inJustDecodeBounds = true ; 22 23 // The second parameter is the parameter passed in when parsing the image. Since there may be too many parameters passed in, all parameters are directly encapsulated into an object 24 BitmapFactory.decodeFile(path, opts); 25 26 // Get 27 int imgWidth = opts.outWidth; 28 int imgHeight = opts.outHeight; 29 30 // Get the width and height of the current phone screen 31 Display dp = getWindowManager().getDefaultDisplay(); 32 int screenWidth = dp. getWidth(); 33 int screenHeight = dp.getHeight(); 34 35 // Set the default zoom ratio to 1 36 int scale = 1 ; 37 38 // Calculate the ratio between the width and height of the picture and the screen width and height, that is, calculate the width scaling ratio, the height scaling ratio is 39 int scaleWidth = imgWidth / screenWidth; 40 int scaleHeight = imgHeight / screenHeight; 41 42 // Select scaling Scale, if the picture is smaller than the screen, it will not be scaled. If the picture is larger than the screen, but the width and height scaling ratio is different, choose a larger scale ratio 43 if (scaleWidth >= scaleHeight && scaleWidth > 1 ) { 44 scale = scaleWidth; 45 } else if (scaleWidth < scaleHeight && scaleHeight > 1 ) { 46 scale = scaleHeight; 47 } 48 // Set the scaling ratio in the Options object 49 opts.inSampleSize = scale; 50 // Be sure to set the inJustDecodeBound field to false, in fact the default value is false, 51 // But it has been changed in the previous code For true, so change it. Of course, you can also re-new an Option object 52 opts.inJustDecodeBounds = false ; 53 Bitmap bm = BitmapFactory.decodeFile(path, opts); 54 iv.setImageBitmap(bm); 55 } 56 }
Simple layout file
1 <?xml version="1.0" encoding="utf-8"?> 2 3 4 5 6 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 7 xmlns:tools="http://schemas.android.com/tools" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 android:orientation="vertical" 11 tools:context="com.hanbao.myapplication.MainActivity" > 12 13 <EditText 14 android:id="@+id/ed" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:gravity="center" 18 android:text="a.jpg" 19 android:textColor="#00ff00" 20 android:textSize="30sp" /> 21 22 <requestFocus /> 23 24 <Button 25 android:onClick="see" 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 29 android:text="Click to watch the movie, you know how!" 28 android:textColor="#00ffff" 30 android:textSize="30sp" /> 31 32 <ImageView 33 android:id="@+id/iv" 34 android:layout_width="match_parent" 35 android:layout_height="match_parent" 36 android:layout_gravity="center" 37 android:src="@drawable/log"/> 38 </LinearLayout>