我正在创建一个我要上传图片的应用程序。在上传之前我必须选择图像或捕获它。如果我从图库中选择一个图像,那么我可以成功上传图像,但当我尝试上传捕获的图像时,未找到捕获的图像路径,请帮我解决我的问题
capture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stubIntent intent=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
startActivityForResult(cameraIntent, 2);
}
});
OnActivity结果代码
if(requestCode==2)
{
Bitmap photo = (Bitmap)data.getExtras().get("data");
pic.setImageBitmap(photo);
String name=(String) data.getExtras().get("data");
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
}
答案 0 :(得分:0)
使用它:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(mCapturedImageURI,
filePathColumn, null, null, null);
cursor.moveToFirst();
int column_index = cursor.getColumnIndex(filePathColumn[0]);
String picpath = cursor.getString(column_index);
cursor.close();
}
答案 1 :(得分:0)
下面的代码将打开相机 -
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PICTURE);
点击图片后会执行以下代码 -
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode == RESULT_OK && requestCode == TAKE_PICTURE) {
if (data != null) {
Bitmap bm = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] ba = bao.toByteArray();
String ba1 = Base64.encodeToString(ba, Base64.DEFAULT);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image", ba1));
try {
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(
"http://..../uploadPic.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i("Excecption", "caught in exception");
e.printStackTrace();
} catch (Exception e) {
Log.i("Excecption", "caught in exception");
e.printStackTrace();
}
}
}
// We need to recycle unused bitmaps
super.onActivityResult(requestCode, resultCode, data);
}