2013-01-11 35 views
0

我點擊圖像按鈕時出現問題打開對話詢問我是否想從圖庫中選擇或拍照。 當我從畫廊中選擇所有正確的圖像時,我將它發送到服務器。 但是,當我選擇相機,我可以拍照,但沒有出現圖片,然後我沒有看到我發送到服務器,如果有的話。 下面的代碼:相機和相冊問題onActivityResult

public class MainActivity extends Activity { 

private final String PHP_URL = "http://wapps.no-ip.org/testservice/service.php"; 
private EditText fldLocation, fldDescription; 
ImageView targetImage, btnLocation; 
Button btnSend; 
JSONParser jParser; 
HttpResponse response; 
Intent intent; 
Bitmap theImage; 
String selectedImagePath; 
Uri imageUri; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.list); 
    // =============================================== 
    // tag elements from layout 
    // =============================================== 
    fldLocation = (EditText) findViewById(R.id.fldLocation); 
    fldDescription = (EditText) findViewById(R.id.fldDescription); 
    btnSend = (Button) findViewById(R.id.btnSend); 
    targetImage = (ImageView) findViewById(R.id.targetimage); 
    btnLocation = (ImageView) findViewById(R.id.btnLocation); 

    registerForContextMenu(btnSend); 
    targetImage.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      openAddPhoto(); 

     } 
    }); 
    btnSend.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      MakePost(); 

     } 
    }); 
} 

public void MakePost() { 

    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(PHP_URL); 

    //targetImage.buildDrawingCache(); 
    //theImage = targetImage.getDrawingCache(); 


    //theImage = BitmapFactory.decodeFile(selectedImagePath); 

    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 

    theImage.compress(Bitmap.CompressFormat.JPEG, 100, stream); 

    byte[] byteArray = stream.toByteArray(); 

    String str = Base64.encodeBytes(byteArray); 

    try { 
     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("addimg", "true")); 
     nameValuePairs.add(new BasicNameValuePair("img", str)); 
     //nameValuePairs.add(new BasicNameValuePair("img",f.getAbsolutePath())); 
     nameValuePairs.add(new BasicNameValuePair("location", fldLocation 
       .getText().toString())); 
     nameValuePairs.add(new BasicNameValuePair("desc", fldDescription 
       .getText().toString())); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute HTTP Post Request 
     httpclient.execute(httppost);// HttpResponse response = 

    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } 
} 

public void openAddPhoto() { 

    AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
    dialog.setTitle("Choose path"); 
    String[] items = { "Camera", "Gallery" }; 
    dialog.setItems(items, new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int id) { 

      if (id == 1) { 
       // call gallery 

       Intent pickPhoto = new Intent(
         Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       startActivityForResult(pickPhoto, 1); 

      } else { 
       // Call camera Intent 

       Intent takePicture = new Intent(
         MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(takePicture, 0); 
      } 

     } 
    }); 

    dialog.setNeutralButton("cancel", 
      new android.content.DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 

        dialog.dismiss(); 
       } 
      }); 
    dialog.show(); 
} 














@Override 
protected void onActivityResult(int requestCode, int resultCode, 
     Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
    case 0: 
     if (resultCode == RESULT_OK) { 

      imageUri = data.getData(); 
      fldDescription.setText(imageUri.toString()); 
      try { 
       theImage = Media.getBitmap(this.getContentResolver(), imageUri); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 

     break; 
    case 1: 
     if (resultCode == RESULT_OK) { 

      imageUri = data.getData(); 
      fldDescription.setText(imageUri.toString()); 
      try { 
       theImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      targetImage.setImageURI(imageUri); 
     } 

     break; 
    } 
} 

}

回答

0

要啓動的意圖,並告訴它在哪裏寫的文件,然後當你知道的意圖返回該文件被保存:

File out = new File(path, somefilename); 
    android.net.Uri uri = android.net.Uri.fromFile(out); 

    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);     
    takePicture.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);   
    startActivityForResult(takePicture, 0); 
+1

我嘗試仍然不工作,當我在景觀模式上拍攝照片時,所有拍攝的照片都完美無缺,我嘗試通過此清單並僅製作應用肖像,但仍然只在風景照片上顯示。 –