做了一个图片上传的程序,
在模拟器里调试:拍照后获得的照片不是刚刚拍的那张,
本地选定的图片可以获得选中图片!在HTC One x里测试:
拍照和本地都获取不了图片!不知道是什么问题:ShareActivity.java
public class ShareActivity extends Activity implements Serializable { public static final int SELECT_PIC_BY_TACK_PHOTO = 1;// Camera
public static final int SELECT_PIC_BY_PICK_PHOTO = 2;// Gallery
// path-KEY
public static final String SAVED_IMAGE_DIR_PATH = "photo_path";
private static final String TAG = "ShareActivity"; // path
private String picPath;
private Intent lastIntent;
private Uri photoUri; private RelativeLayout layout1;
private RelativeLayout layout2; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_share); layout1 = (RelativeLayout) this.findViewById(R.id.upload_photo);
layout2 = (RelativeLayout) this.findViewById(R.id.upload_gallery);
lastIntent = getIntent(); layout1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
takePhoto();
}
});
layout2.setOnClickListener(new OnClickListener() { public void onClick(View v) {
pickPhoto();
}
});
} private void takePhoto() { String SDState = Environment.getExternalStorageState();
if (SDState.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

ContentValues values = new ContentValues();
photoUri = this.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
} else {
Toast.makeText(this, R.string.takePhoto_msg, Toast.LENGTH_LONG)
.show();
}
} private void pickPhoto() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
doPhoto(requestCode, data);
}
super.onActivityResult(requestCode, resultCode, data);
} private void doPhoto(int requestCode, Intent data) { if (requestCode == SELECT_PIC_BY_PICK_PHOTO) {
if (data == null) {
Toast.makeText(this, "选择图片文件出错", Toast.LENGTH_LONG).show();
return;
}
photoUri = data.getData();
if (photoUri == null) {
Toast.makeText(this, "选择图片文件出错", Toast.LENGTH_LONG).show();
return;
}
}
String[] pojo = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(photoUri, pojo, null, null, null);
if (cursor != null) {
int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
cursor.moveToFirst();
picPath = cursor.getString(columnIndex);
cursor.close();
}
Log.i(TAG, "imagePath = " + picPath);
if (picPath != null) { Intent startEx = new Intent(ShareActivity.this, PhotoPre.class);
Bundle bundle = new Bundle();
bundle.putString(SAVED_IMAGE_DIR_PATH, picPath);
startEx.putExtras(bundle);
setResult(Activity.RESULT_FIRST_USER, startEx);
startActivity(startEx);
} else {
Toast.makeText(this, "选择文件不正确!", Toast.LENGTH_LONG).show(); } }
}预览:PhotoPre.java 
public class PhotoPre extends Activity {
public static String picPath = null;
private static final String TAG = "PhotoPreActivity"; public static final String KEY_PHOTO_PATH = "photo_path";
public static final String IMAGE_UNSPECIFIED = "image/*"; private ProgressDialog progressDialog;
private Button back;
private Button upload;
private Button cancel;
private ImageView view;
private Bitmap bm; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageshow); back = (Button) this.findViewById(R.id.phtot_back);
upload = (Button) this.findViewById(R.id.phtot_upload);
cancel = (Button) this.findViewById(R.id.phtot_cancel);
view = (ImageView) this.findViewById(R.id.photo_selected);
progressDialog = new ProgressDialog(this); back.setOnClickListener(new ThisOnClickListener(0));
upload.setOnClickListener(new ThisOnClickListener(1));
cancel.setOnClickListener(new ThisOnClickListener(2)); Bundle bundle = getIntent().getExtras();
String path = bundle.getString(KEY_PHOTO_PATH);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bm = BitmapFactory.decodeFile(path);
view.setImageBitmap(bm);
} protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (requestCode == 1) {
view.setImageBitmap(null);
picPath = data.getStringExtra(ShareActivity.SAVED_IMAGE_DIR_PATH);
Log.i(TAG, "最终选择的图片=" + picPath);
Bitmap bm = BitmapFactory.decodeFile(picPath);
view.setImageBitmap(bm);
} else if (requestCode == 2) { try {
Bitmap bm = null;
ContentResolver resolver = getContentResolver();
Uri uri = data.getData();
bm = MediaStore.Images.Media.getBitmap(resolver, uri); String[] pro = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, pro, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String picPath1 = cursor.getString(column_index);
Bitmap bitmap = BitmapFactory.decodeFile(picPath1);
view.setImageBitmap(bitmap);
cursor.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, e.toString());
}
}
}
super.onActivityResult(requestCode, resultCode, data);
} public class ThisOnClickListener implements OnClickListener {
private int index = 0; private ThisOnClickListener(int i) {
index = i;
} @Override
public void onClick(View v) {
switch (index) {
case 0:
Intent intent = new Intent(PhotoPre.this, MainActivity.class);
startActivity(intent); 
break;
case 1:
try {
postFile();
} catch (Exception e) {
e.printStackTrace();
}
break;
case 2:
Intent intent1 = new Intent(PhotoPre.this, ShareActivity.class);
startActivity(intent1);
break;
} } } private void postFile() throws Exception {
progressDialog.setMessage("正在上传文件...");
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(
CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost httppost = new HttpPost("http://localhost:9002/upload.php");
File file = new File("c:/TRASH/zaba_1.jpg"); FileEntity reqEntity = new FileEntity(file, "binary/octet-stream"); httppost.setEntity(reqEntity);
reqEntity.setContentType("binary/octet-stream");
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity(); System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
} httpclient.getConnectionManager().shutdown();
}}
android

解决方案 »

  1.   

    应该是路径的问题,我遇到过在HTC机器上出现打开sdcrad卡返回图片的Uri异常的情况
    你先打印一下路径看看
      

  2.   

    在获得1.14M的图片时抛java.lang.OutOfMemoryError
      

  3.   

    http://download.csdn.net/detail/shen332401890/5743813 看看这个demo 是否可以 得到数据,这个是我亲测成功的
      

  4.   

    抛java.lang.OutOfMemoryError,打印的路径是没有错的
      

  5.   

    OOM 是你讲数据create 成bitmap 的时候出的问题 注意压缩 压缩就不用多说了吧 网上很多教程
      

  6.   

    你刚才发的Demo,拍照后显示出来 的照片是横向的,比如是竖屏拍照,预览是横向显示了
      

  7.   

    那个需要你exif图片心理里面修正一下 orientation数据 
      

  8.   

    拍照的问题最好还是用 Camera 对象解决,有 takePicture 方法直接获取图像数据,想存哪里就存那里。