我正在尝试从下面给出的代码中捕获3个图像。问题是,当我在我的模拟器上运行它运行正常并分别在imageviews中显示图像但当我尝试在真实设备上运行它(如三星galaxy neo3 lite等)时,它会在一次图像捕获后说“不幸的是应用程序”已停止“。
我提供以下代码。请帮助我解决问题。
public class LodgeComplaints2 extends Activity实现OnItemClickListener {
private static final String NAMESPACE="http://tempuri.org/";
//private static final String URL="webservice url";
private static final String URL="webservice url";
private static final String SOAP_ACTION="htxxx";
private static final String METHOD_NAME="xxxx";
private static final int CAMERA_PIC_REQUEST=1111;
private static int RESULT_LOAD_IMAGE = 1;
private static final int PICK_FROM_GALLERY = 2;
Button btnSend,btnImage,btnPending;
TextView txtComplCat;
Bitmap thumbnail=null;
static String address2;
private double latitude;
private double longitude;
Location location;
ApplicationLocationProvider appLocprov;
String am_pm,time;
String Comp_Name;
Bundle b;
NotificationManager nm;
EditText edtOthers,edtAddr2;
private String complaint;
ImageView imgVw,imgVw1,imgVw2,imgVw3;
Bitmap thumb2=null,thumb3=null,thumb4=null;
private int countImage=1;
private static final String LOG_TAG = "Google Places Autocomplete";
private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String OUT_JSON = "/json";
private static final String API_KEY = "xxxxxxxxxxxxxxxxxxxxxxx";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_lodge_complaints2);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);}
if(savedInstanceState==null)
{
b=getIntent().getExtras();
if(b==null)
{
Comp_Name=null;
}
else
{
Comp_Name=b.getString("Complaint");
}
}
else
{
Comp_Name=(String)savedInstanceState.getSerializable("Complaint");
}
txtComplCat=(TextView)findViewById(R.id.txtCompCat);
imgVw1=(ImageView)findViewById(R.id.imgComp1);
imgVw2=(ImageView)findViewById(R.id.imgComp2);
imgVw3=(ImageView)findViewById(R.id.imgComp3);
if(Comp_Name.equals("Others"))
{
edtOthers=(EditText)findViewById(R.id.txtOther);
edtOthers.setVisibility(View.VISIBLE);
//complaint=edtOthers.getText().toString();
}
else
{
txtComplCat.setText(Comp_Name);
//complaint=txtComplCat.getText().toString();
}
edtAddr2=(EditText)findViewById(R.id.txtLocation);
/** imgVw=(ImageView)findViewById(R.id.imgExit);
imgVw.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent startMain=new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
});
**/
btnImage=(Button)findViewById(R.id.btnClick);
btnSend=(Button)findViewById(R.id.btnSend);
btnImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final CharSequence[] items={"Choose from Gallery","Capture an Image"};
AlertDialog.Builder builder=new AlertDialog.Builder(LodgeComplaints2.this);
builder.setTitle("Make your Selection");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if(items[which].equals("Choose from Gallery"))
{
Intent inGal=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(inGal,RESULT_LOAD_IMAGE);
}
else if(items[which].equals("Capture an Image"))
{
Intent inCap=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(inCap, CAMERA_PIC_REQUEST);
}
}
});
AlertDialog alert=builder.create();
alert.show();
}
});
btnSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final CharSequence[] items2={"Send Now"};
AlertDialog.Builder builder2=new AlertDialog.Builder(LodgeComplaints2.this);
builder2.setTitle("Make your selection");
builder2.setItems(items2, new DialogInterface.OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if(items2[which].equals("Send Now"))
{
String encoded;
if(thumbnail==null){
Bitmap bmp=retreivePic();
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bmp.compress(CompressFormat.JPEG, 100, bos);
byte[] imageBytes = bos.toByteArray();
encoded = Base64.encodeToString(imageBytes, Base64.DEFAULT);
if(countImage==1)
thumb2=bmp;
else if(countImage==2)
thumb3=bmp;
else if(countImage==3)
thumb4=bmp;
}
else
{
ByteArrayOutputStream bos=new ByteArrayOutputStream();
thumbnail.compress(CompressFormat.JPEG, 100, bos);
byte[] imageBytes = bos.toByteArray();
encoded = Base64.encodeToString(imageBytes, Base64.DEFAULT);
if(countImage==1)
thumb2=thumbnail;
else if(countImage==2)
thumb3=thumbnail;
else if(countImage==3)
thumb4=thumbnail;
thumbnail=null;
}
TelephonyManager tMgr=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String number=tMgr.getLine1Number();
final Calendar c=Calendar.getInstance();
int hour=c.get(Calendar.HOUR_OF_DAY);
int minute=c.get(Calendar.MINUTE);
if (c.get(Calendar.AM_PM) == Calendar.AM)
am_pm = "AM";
else if (c.get(Calendar.AM_PM) == Calendar.PM)
am_pm = "PM";
time=hour+":"+minute+""+am_pm;
//Date date=new Date();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
//String formattedDate = df.format(c.getTime());
String formattedDate =df.format(date);
SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(LodgeComplaints2.this);
String name=preferences.getString("Name", "");
String ph_nm=preferences.getString("Number", "");
PropertyInfo pi=new PropertyInfo();
pi.type=pi.STRING_CLASS;
pi.name="recDate";
PropertyInfo pi2=new PropertyInfo();
pi2.type=pi2.STRING_CLASS;
pi2.name="recTime";
PropertyInfo pi3=new PropertyInfo();
pi3.type=pi3.STRING_CLASS;
pi3.name="compBy";
PropertyInfo pi4=new PropertyInfo();
pi4.type=pi4.STRING_CLASS;
pi4.name="compName";
PropertyInfo pi5=new PropertyInfo();
pi5.type=pi5.STRING_CLASS;
pi5.name="compAddr";
PropertyInfo pi6=new PropertyInfo();
pi6.type=pi6.STRING_CLASS;
pi6.name="compMob";
PropertyInfo pi7=new PropertyInfo();
pi7.type=pi7.STRING_CLASS;
pi7.name="image";
PropertyInfo pi8=new PropertyInfo();
pi8.type=pi8.STRING_CLASS;
pi8.name="compAddr2";
request.addProperty(pi,formattedDate);
request.addProperty(pi2,time);
request.addProperty(pi3,complaint);
request.addProperty(pi4,name);
request.addProperty(pi5,getAddress(latitude, longitude));
request.addProperty(pi6,ph_nm);
request.addProperty(pi7,encoded);
request.addProperty(pi8,edtAddr2.getText().toString());
SoapSerializationEnvelope env=new SoapSerializationEnvelope(SoapEnvelope.VER11);
env.dotNet=true;
env.setOutputSoapObject(request);
HttpTransportSE htse=new HttpTransportSE(URL);
try{
htse.debug=true;
htse.call(SOAP_ACTION, env);
Object response=env.getResponse();
//TextView txt2=(TextView)findViewById(R.id.txtMsg3);
//txt2.setText("Sent");
Toast.makeText(getBaseContext(), "Sent", Toast.LENGTH_LONG).show();
//Toast.makeText(getBaseContext(), encoded, Toast.LENGTH_LONG).show();
//TextView txt5=(TextView)findViewById(R.id.txtMsg5);
//txt5.setText(address2);
}
catch(Exception exc)
{
System.out.println("Dump : "+htse.responseDump);
//Toast.makeText(getBaseContext(), htse.responseDump, Toast.LENGTH_LONG).show();
txtComplCat.setText(htse.responseDump);
}
}
}
});
AlertDialog alert2=builder2.create();
alert2.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.lodge_complaints2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==CAMERA_PIC_REQUEST && resultCode==RESULT_OK && data!=null)
{
Bitmap thumb=(Bitmap)data.getExtras().get("data");
//img.setImageBitmap(thumb);
ByteArrayOutputStream bytes=new ByteArrayOutputStream();
thumb.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
if(countImage==1)
{
imgVw1.setImageBitmap(thumb);
}
else if(countImage==2)
{
imgVw2.setImageBitmap(thumb);
}
else if(countImage==3)
{
imgVw3.setImageBitmap(thumb);
}
countImage++;
File file=new File(Environment.getExternalStorageDirectory()+File.separator+"image.jpg");
try{
file.createNewFile();
FileOutputStream fo=new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
else if(requestCode==RESULT_LOAD_IMAGE && resultCode==RESULT_OK && null!=data)
{
Uri selectedImage=data.getData();
String [] filepathColumn={MediaStore.Images.Media.DATA};
Cursor cursor=getContentResolver().query(selectedImage, filepathColumn, null, null, null);
cursor.moveToLast();
int columnIndex=cursor.getColumnIndex(filepathColumn[0]);
String picturePath=cursor.getString(columnIndex);
cursor.close();
thumbnail=(BitmapFactory.decodeFile(picturePath));
if(countImage==1){
imgVw1.setImageBitmap(thumbnail);
}
else if(countImage==2)
{
imgVw2.setImageBitmap(thumbnail);
}
else if(countImage==3)
{
imgVw3.setImageBitmap(thumbnail);
}
countImage++;
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public Bitmap retreivePic()
{
File f=new File(Environment.getExternalStorageDirectory()+File.separator+"image.jpg");
Bitmap bmp=BitmapFactory.decodeFile(f.getAbsolutePath());
return bmp;
}
}
}
}