描述: 通过拍照后,将图片显示在Listview中。 问题:1,在模拟器上运行正常 2,在我手机上运行,拍照后显示,程序会crash掉 3,在平板上运行,拍前两张照片,listview上显示都正常,而第三张时,程序crash.代码如下://拍照的程序import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;import org.apache.http.client.ClientProtocolException;import bunny.android.Class.ThreadOfCallInterface;import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Message;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;public class CtssPictureFactory extends CtssBaseActivity implements OnClickListener,iPictureFactory { private ListView listOfPictures=null;
private CtssPictureFactoryType mFactoryType;
private TextView tvFactoryName=null;
private EditText editLogMemo=null;
private Button btnSubmitPictureData=null;
private Button btnTakePhoto=null;
private List<HashMap<String,Object>> mListOfPictureData=new ArrayList<HashMap<String,Object>>();
private CtssPictureFactoryAdapter pictureAdapter=null;
private final int RESULT_CAPTURE_IMAGE=0;
private String mPhotoPath="";
private int mPicturePostion=-1;
private Spinner spinProject=null;
private ArrayAdapter<String> PojectAdapter;
private ArrayList<String> ProjectNameList=new ArrayList<String>();
private String mProjectId="";
private String mGpsData="0;0";
private String mLogtitle="";
private String mLogMemo="";
private String mPicturePathData="";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.picturefactory);
listOfPictures= (ListView) findViewById(R.id.listViewPictureFactory);
View headerView= LayoutInflater.from(this).inflate(R.layout.picturefactory_header, null);
listOfPictures.addHeaderView(headerView);
//创建adapter,显示图片
pictureAdapter=new CtssPictureFactoryAdapter(this,mListOfPictureData,this);
listOfPictures.setAdapter(pictureAdapter);
tvFactoryName= (TextView) findViewById(R.id.tvFactoryName);
//the bellow for button
btnSubmitPictureData=(Button) findViewById(R.id.btnSubmitPicture);
btnSubmitPictureData.setOnClickListener(this);
btnTakePhoto= (Button) findViewById(R.id.btnTakePhoto);
btnTakePhoto.setOnClickListener(this);
editLogMemo=(EditText) findViewById(R.id.editPictureFactoryMemo);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
HashMap<String,Object> mapObj=new HashMap<String,Object>();
mapObj.put(CtssPictureFactoryMapName.fPicturePath, this.mPhotoPath);
mapObj.put(CtssPictureFactoryMapName.fPictureIndex, (mListOfPictureData.size()+1)+"");
if(mPicturePostion==-1)
mListOfPictureData.add(mapObj);//add new picture
else
mListOfPictureData.get(mPicturePostion).put(CtssPictureFactoryMapName.fPicturePath, this.mPhotoPath);//update picture
pictureAdapter.notifyDataSetChanged();
mPicturePostion=-1;
} } public void onClick(View v) {
// TODO Auto-generated method stub
if(v==btnTakePhoto)
{
takePhoto();//拍照
}
if(this.btnSubmitPictureData==v)
{
readLogData();
if(mProjectId=="" || mListOfPictureData.size()==0)
{
return;
}
mThread = new ThreadOfCallInterface("threadOfUploadLogimg", this);
mThread.start();
}
}
private void takePhoto()
{
Intent imageCaptureIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
mPhotoPath = Environment.getExternalStorageDirectory()
.toString() + "/CONSDCGMPIC/";//
String fileName = "pad"
+ new SimpleDateFormat("yyyyMMddHHmmss")
.format(new Date()) + ".jpg";// 照片命名
File out = new File(mPhotoPath);
if (!out.exists()) {
out.mkdirs();
}
out = new File(mPhotoPath, fileName);
mPhotoPath = mPhotoPath + fileName;// 该照片的绝对路径
Uri uri = Uri.fromFile(out);
imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(imageCaptureIntent,
RESULT_CAPTURE_IMAGE);
}public void reTakePhoto(int position) {
// TODO Auto-generated method stub
mPicturePostion=position;
takePhoto();
}public void deletePhoto(int position) {
// TODO Auto-generated method stub
mListOfPictureData.remove(position);
pictureAdapter.notifyDataSetChanged();
}}package bunny.JianAnWang;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;public class CtssPictureFactoryAdapter extends BaseAdapter { private List<HashMap<String, Object>> mPictureList = null;
private Context mContext = null;
private iPictureFactory mFactoryInterface=null; public CtssPictureFactoryAdapter(Context view,
List<HashMap<String, Object>> pictureList,iPictureFactory interfaceOfPictureFactory) {
mPictureList = pictureList;
mContext = view;
mFactoryInterface=interfaceOfPictureFactory;
} public int getCount() {
// TODO Auto-generated method stub
return mPictureList==null?0: mPictureList.size();
} public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
} public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
} public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater vi = null;
vi = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout childView = (LinearLayout) vi.inflate(
R.layout.picturefacotry_item, parent, false);
// find component in view
TextView tvPictureIndex = (TextView) childView
.findViewById(R.id.tvPictureFactoryPictureIndex);
ImageView iv = (ImageView) childView.findViewById(R.id.ImgPicture);
String picturePath = (String) mPictureList.get(position).get(
CtssPictureFactoryMapName.fPicturePath);
String pictureInex = (String) mPictureList.get(position).get(
CtssPictureFactoryMapName.fPictureIndex); tvPictureIndex.setText(pictureInex);
File fImg = new File(picturePath);
if (fImg.exists()) {
FileInputStream fis = null;
try {
fis = new FileInputStream(picturePath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
// e.printStackTrace(); }
Bitmap bm = BitmapFactory.decodeStream(fis);
iv.setImageBitmap(bm);
}
//button
Button btnRetakePhoto= (Button) childView.findViewById(R.id.btnReTakePhoto);
btnRetakePhoto.setOnClickListener(new OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
mFactoryInterface.reTakePhoto(position);
}
});
Button btnDeletePicture= (Button) childView.findViewById(R.id.btnDeletePiture);
btnDeletePicture.setOnClickListener(new OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
mFactoryInterface.deletePhoto(position);
}
});
return childView;
}}
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;import org.apache.http.client.ClientProtocolException;import bunny.android.Class.ThreadOfCallInterface;import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Message;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;public class CtssPictureFactory extends CtssBaseActivity implements OnClickListener,iPictureFactory { private ListView listOfPictures=null;
private CtssPictureFactoryType mFactoryType;
private TextView tvFactoryName=null;
private EditText editLogMemo=null;
private Button btnSubmitPictureData=null;
private Button btnTakePhoto=null;
private List<HashMap<String,Object>> mListOfPictureData=new ArrayList<HashMap<String,Object>>();
private CtssPictureFactoryAdapter pictureAdapter=null;
private final int RESULT_CAPTURE_IMAGE=0;
private String mPhotoPath="";
private int mPicturePostion=-1;
private Spinner spinProject=null;
private ArrayAdapter<String> PojectAdapter;
private ArrayList<String> ProjectNameList=new ArrayList<String>();
private String mProjectId="";
private String mGpsData="0;0";
private String mLogtitle="";
private String mLogMemo="";
private String mPicturePathData="";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.picturefactory);
listOfPictures= (ListView) findViewById(R.id.listViewPictureFactory);
View headerView= LayoutInflater.from(this).inflate(R.layout.picturefactory_header, null);
listOfPictures.addHeaderView(headerView);
//创建adapter,显示图片
pictureAdapter=new CtssPictureFactoryAdapter(this,mListOfPictureData,this);
listOfPictures.setAdapter(pictureAdapter);
tvFactoryName= (TextView) findViewById(R.id.tvFactoryName);
//the bellow for button
btnSubmitPictureData=(Button) findViewById(R.id.btnSubmitPicture);
btnSubmitPictureData.setOnClickListener(this);
btnTakePhoto= (Button) findViewById(R.id.btnTakePhoto);
btnTakePhoto.setOnClickListener(this);
editLogMemo=(EditText) findViewById(R.id.editPictureFactoryMemo);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
HashMap<String,Object> mapObj=new HashMap<String,Object>();
mapObj.put(CtssPictureFactoryMapName.fPicturePath, this.mPhotoPath);
mapObj.put(CtssPictureFactoryMapName.fPictureIndex, (mListOfPictureData.size()+1)+"");
if(mPicturePostion==-1)
mListOfPictureData.add(mapObj);//add new picture
else
mListOfPictureData.get(mPicturePostion).put(CtssPictureFactoryMapName.fPicturePath, this.mPhotoPath);//update picture
pictureAdapter.notifyDataSetChanged();
mPicturePostion=-1;
} } public void onClick(View v) {
// TODO Auto-generated method stub
if(v==btnTakePhoto)
{
takePhoto();//拍照
}
if(this.btnSubmitPictureData==v)
{
readLogData();
if(mProjectId=="" || mListOfPictureData.size()==0)
{
return;
}
mThread = new ThreadOfCallInterface("threadOfUploadLogimg", this);
mThread.start();
}
}
private void takePhoto()
{
Intent imageCaptureIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
mPhotoPath = Environment.getExternalStorageDirectory()
.toString() + "/CONSDCGMPIC/";//
String fileName = "pad"
+ new SimpleDateFormat("yyyyMMddHHmmss")
.format(new Date()) + ".jpg";// 照片命名
File out = new File(mPhotoPath);
if (!out.exists()) {
out.mkdirs();
}
out = new File(mPhotoPath, fileName);
mPhotoPath = mPhotoPath + fileName;// 该照片的绝对路径
Uri uri = Uri.fromFile(out);
imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(imageCaptureIntent,
RESULT_CAPTURE_IMAGE);
}public void reTakePhoto(int position) {
// TODO Auto-generated method stub
mPicturePostion=position;
takePhoto();
}public void deletePhoto(int position) {
// TODO Auto-generated method stub
mListOfPictureData.remove(position);
pictureAdapter.notifyDataSetChanged();
}}package bunny.JianAnWang;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;public class CtssPictureFactoryAdapter extends BaseAdapter { private List<HashMap<String, Object>> mPictureList = null;
private Context mContext = null;
private iPictureFactory mFactoryInterface=null; public CtssPictureFactoryAdapter(Context view,
List<HashMap<String, Object>> pictureList,iPictureFactory interfaceOfPictureFactory) {
mPictureList = pictureList;
mContext = view;
mFactoryInterface=interfaceOfPictureFactory;
} public int getCount() {
// TODO Auto-generated method stub
return mPictureList==null?0: mPictureList.size();
} public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
} public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
} public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater vi = null;
vi = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout childView = (LinearLayout) vi.inflate(
R.layout.picturefacotry_item, parent, false);
// find component in view
TextView tvPictureIndex = (TextView) childView
.findViewById(R.id.tvPictureFactoryPictureIndex);
ImageView iv = (ImageView) childView.findViewById(R.id.ImgPicture);
String picturePath = (String) mPictureList.get(position).get(
CtssPictureFactoryMapName.fPicturePath);
String pictureInex = (String) mPictureList.get(position).get(
CtssPictureFactoryMapName.fPictureIndex); tvPictureIndex.setText(pictureInex);
File fImg = new File(picturePath);
if (fImg.exists()) {
FileInputStream fis = null;
try {
fis = new FileInputStream(picturePath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
// e.printStackTrace(); }
Bitmap bm = BitmapFactory.decodeStream(fis);
iv.setImageBitmap(bm);
}
//button
Button btnRetakePhoto= (Button) childView.findViewById(R.id.btnReTakePhoto);
btnRetakePhoto.setOnClickListener(new OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
mFactoryInterface.reTakePhoto(position);
}
});
Button btnDeletePicture= (Button) childView.findViewById(R.id.btnDeletePiture);
btnDeletePicture.setOnClickListener(new OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub
mFactoryInterface.deletePhoto(position);
}
});
return childView;
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货