在本博客的舊文章"
通過EXTRA_OUTPUT, 指定儲存圖像的路徑"中, 描述了如何指定儲存圖像的路徑, 但沒有描述如何顯示拍攝的圖像. 本文將描述如何在onActivityResult()通過 BitmapFactory 顯示拍攝的圖像.
package com.AndroidImageCapture;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class AndroidImageCaptureActivity extends Activity {
ImageView imageiewImageCaptured;
String strImage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonImageCapture = (Button)findViewById(R.id.captureimage);
imageiewImageCaptured = (ImageView)findViewById(R.id.imagecaptured);
buttonImageCapture.setOnClickListener(buttonImageCaptureOnClickListener);
}
Button.OnClickListener buttonImageCaptureOnClickListener
= new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
//Specify the path of the captured image
strImage = Environment.getExternalStorageDirectory().getAbsolutePath()+"/mypicture.jpg";
File myImage = new File(strImage);
Uri uriMyImage = Uri.fromFile(myImage);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uriMyImage);
startActivityForResult(intent, 0);
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Bitmap bmp = BitmapFactory.decodeFile(strImage);
imageiewImageCaptured.setImageBitmap(bmp);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/captureimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Call for ACTION_IMAGE_CAPTURE"
/>
<ImageView
android:id="@+id/imagecaptured"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
沒有留言:
發佈留言