首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android显示图片有关问题

2012-05-14 
android显示图片问题我想定一段代码,在一个界面 中有一个ImageView但是有两个ImageButton,一个负责打开文

android显示图片问题
我想定一段代码,在一个界面 中有一个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); 
  } 


  class MyButton2Listener implements OnClickListener {
 
  @Override
  public void onClick (View v)
  {
  //TODO Auto-generated method stub
  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  /* 开启Pictures画面Type设定为image */ 
  startActivityForResult(intent, 1);
  b=1;
  }
  }
  //@Override
  protected void onActivityResult1(int requestCode, int resultCode, Intent data)
  {
  super.onActivityResult(requestCode, resultCode, data);
  Bundle bundle = data.getExtras();
  Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
  img1.setImageBitmap(bitmap);
   
}

   

}

[解决办法]
java.lang.NullPointerException 表示空数据

出错位置:
at com.tmxk.second.onActivityResult(second.java:74)
也就是在second.java这个文件的第74行。位于onActivityResult里面,也就是返回时未获得数据。

[解决办法]
楼主是否可以考虑换一种思路?
我也调用过摄像程序拍照
我的方法是,调用时,传参告诉摄像程序,文件要存储在内存卡中的哪个路径
然后,如果摄像程序返回结果是拍摄成功的话
根据前面的那个路径,从内存卡中的文件载入图像


我之前有遇到一个问题,
就是返回结果,那个data的值为null
我猜楼主也是这个问题,这个断点调试一下就知道了

热点排行