Android大画像ロードおよびEXIF情報読み込み書き込み

4339 ワード

<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:
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+"
"+model+"
"+iso+"
"+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(); } }