我想定一段代码,在一个界面 中有一个ImageView但是有两个ImageButton,一个负责打开文件寻找Sd卡中图片并把它显示在ImageView中。如果没有合格图片用户可以调用照像机照像将照片放在ImageView中显示,请教各位大虾们程序该如何写。
附上我写的一段代码,它老报The application has stopped unexception
package com.tmxk;
import java.io.FileNotFoundException;
import com.yarin.android.FileManager.FileManager;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
public class second extends Activity{
/** Called when the activity is first created. */
public String location;
private ImageButton btn1;
private ImageButton btn2;
private ImageButton btn3;
private ImageButton btn4;
private ImageButton btn5;
private EditText edt;
private EditText edt1;
private ImageView img1;
static int b=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
location =intent.getStringExtra("location");
findview();
btn1.setOnClickListener(new MyButtonListener());//打开gallay文件
btn2.setOnClickListener(new MyButton2Listener());//照像打开照像机
}
public void findview(){
btn1 = (ImageButton) findViewById(R.id.btn1);
btn2 = (ImageButton) findViewById(R.id.btn2);
btn3 = (ImageButton) findViewById(R.id.btn3);
btn4 = (ImageButton) findViewById(R.id.btn4);
btn5 = (ImageButton) findViewById(R.id.btn5);
edt = (EditText) findViewById(R.id.edt0);
edt1 =(EditText) findViewById(R.id.edt1);
img1 = (ImageView) findViewById(R.id.img1);
}
class MyButtonListener implements OnClickListener {
@Override
public void onClick (View v)
{
//TODO Auto-generated method stub
Intent intent = new Intent();
/* 开启Pictures画面Type设定为image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT这个Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片后返回本画面 */
startActivityForResult(intent, 1);
b=1;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.e("uri", uri.toString());
ContentResolver cr = this.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
//ImageView imageView = (ImageView) findViewById(R.id.img1);
/* 将Bitmap设定到ImageView */
img1.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(),e);
}
}
super.onActivityResult(requestCode, resultCode, data);
}