<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="load" android:text="加載圖片到內存" /> <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
MAIN AC: java
package com.example.loadimg; import java.io.IOException; import android.media.ExifInterface; import android.nfc.Tag; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.content.Intent; import android.content.DialogInterface.OnClickListener; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.graphics.Point; import android.view.Menu; import android.view.View; import android.view.WindowManager; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private ImageView iv; private int WindowWidth; private int WindowHeight; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); WindowHeight = wm.getDefaultDisplay().getHeight(); WindowWidth = wm.getDefaultDisplay().getWidth(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void load(View v) throws IOException { // 圖片解析配置 Options options = new Options(); // 不去真的解析圖片 獲取圖片頭部信息 options.inJustDecodeBounds = true; Bitmap mitmap = BitmapFactory.decodeFile("/sdcard/test.bmp", options); int height = options.outHeight; int width = options.outWidth; System.out.println("寬" + width + "高" + height); // 獲得手機屏幕寬高比 int scaleX=width/WindowHeight; int scaleY=height/WindowHeight; int scale=1; if(scaleX>scaleY && scaleY>=1) { scale=scaleX; } if(scaleY>scaleX && scaleX>=1) { scale=scaleY; } options.inJustDecodeBounds=false; options.inSampleSize=scale; mitmap = BitmapFactory.decodeFile("/sdcard/test.bmp", options); iv.setImageBitmap(mitmap); readEXIF("/sdcard/test.bmp"); } private void readEXIF(String path) throws IOException { ExifInterface exif=new ExifInterface(path); //獲取信息 String time=exif.getAttribute(ExifInterface.TAG_DATETIME); String model=exif.getAttribute(ExifInterface.TAG_MODEL); String iso=exif.getAttribute(ExifInterface.TAG_ISO); //設置信息 tag能夠自定義 exif.setAttribute(ExifInterface.TAG_EXPOSURE_TIME, 100+""); exif.saveAttributes(); String ex_time=exif.getAttribute(ExifInterface.TAG_EXPOSURE_TIME); AlertDialog.Builder builder=new Builder(this); builder.setTitle("EXIF信息"); builder.setMessage(time+"\n"+model+"\n"+iso+"\n"+ex_time); builder.setPositiveButton("取消",new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.setNegativeButton("肯定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } }