之前开发了一个图片功能,就是即可以拍照,又可以直接调用系统内置Gallery程序选择图片,然后将图片显示在自己的程序ImageView中。
该功能在华为T8620可以正常使用(Android4.0),但是最近试了另一款康佳的v926(Android 2.3.5),却无法从Gallery的图片选择界面中选中的图片显示在ImageView中(会一直让你选,没反应),测试代码发现竟然不肯回调函数 onActivityResult()!  所以只能到这来请教大牛们了,望指点~~
主要代码:
private void doPickPhotoAction() {  
        Context context = PicPostActivity.this; 
        // Wrap our context to inflate list items using correct theme  
        final Context dialogContext = new ContextThemeWrapper(context,  
                android.R.style.Theme_Light);  
        String cancel="返回";  
        String[] choices;  
        choices = new String[2];
        choices[0] = getString(R.string.pick_photo);  //从相册中选择  
        choices[1] = getString(R.string.take_photo);  //拍照  
        ListAdapter adapter = new ArrayAdapter<String>(dialogContext,  
                android.R.layout.simple_list_item_1, choices);  
      
        AlertDialog.Builder builder = new AlertDialog.Builder(dialogContext);  
        builder.setTitle(R.string.attach_picture_from);  
        builder.setSingleChoiceItems(adapter, -1,  
                new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int which) {  
                        dialog.dismiss();  
                        switch (which) {  
                        case 0:  
                         HengtuoUtil.log(0, TAG, "选择相册选择");
                            doPickPhotoFromGallery();// 从相册中去获取  
                            break;  
                        case 1:{  
                            String status=Environment.getExternalStorageState();  
                            if(status.equals(Environment.MEDIA_MOUNTED)){//判断是否有SD卡
                            PHOTO_DIR = new File(Constants.PHOTO_DIR_SDK);
                             HengtuoUtil.log(0, TAG, "选择相机拍摄");
                                doTakePhoto();// 用户点击了从照相机获取  
                            }  
                            else{  
                             //PicPostActivity.showToast("没有SD卡"); 
                             //无SD卡则直接选择手机
                             PHOTO_DIR = new File(Constants.PHOTO_DIR_PHONE);
                                HengtuoUtil.log(3, TAG, "没有SD卡");
                            }  
                            break;                                
                        }
                        }
                    }
                });  
        builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {        
            public void onClick(DialogInterface dialog, int which) {  
                dialog.dismiss();  
            }                
        });  
        AlertDialog dialog = builder.create();
        dialog.show();  
    }  /** 
* 拍照获取图片 
*/  
protected void doTakePhoto() {  
    try {  
        // Launch camera to take photo for selected contact  
        PHOTO_DIR.mkdirs();// 创建照片的存储目录  
        mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName());// 给新照的照片文件命名  
        final Intent intent = getTakePickIntent(mCurrentPhotoFile);  
        startActivityForResult(intent, CAMERA_WITH_DATA);  
    } catch (ActivityNotFoundException e) {  
        Toast.makeText(this, "创建照片失败",Toast.LENGTH_LONG).show();
        HengtuoUtil.log(3, TAG, "创建照片失败");
    }  
}  
  
public static Intent getTakePickIntent(File f) {  
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);  
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));  
    HengtuoUtil.log(1, "hengtuo.PicPostActivity", "拍照成功");
    return intent;  
}  
  
/** 
* 用当前时间给取得的图片命名 
*/  
private String getPhotoFileName() {  
    Date date = new Date(System.currentTimeMillis());  
    SimpleDateFormat dateFormat = new SimpleDateFormat(  
            "'IMG'_yyyyMMdd_HHmmss");  
    return dateFormat.format(date) + ".jpg";  
}  
/**
 *  请求Gallery程序  
 */
protected void doPickPhotoFromGallery() {  
    try {  
        // Launch picker to choose photo for selected contact  
        final Intent intent = getPhotoPickIntent();  
        startActivityForResult(intent, PHOTO_CUT_WITH_DATA);  
    } catch (ActivityNotFoundException e) {  
        Toast.makeText(this, getString(R.string.photoPickerNotFoundText), Toast.LENGTH_LONG).show();
        HengtuoUtil.log(3, TAG, getString(R.string.photoPickerNotFoundText));
    }  
}  
  
/**
 *  封装请求Gallery的intent  
 * @return
 */
public static Intent getPhotoPickIntent() {  
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);  
    intent.setType("image/*");  
    intent.putExtra("crop", "true");  
    intent.putExtra("aspectX", 1);  
    intent.putExtra("aspectY", 1);  
    intent.putExtra("outputX", 600);  
    intent.putExtra("outputY", 400);  
    intent.putExtra("return-data", true);  
    return intent;  
}  
  
/**
 *  因为调用了Camera和Gally所以要判断他们各自的返回情况,他们启动时是这样的startActivityForResult  
 */
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
if(resultCode == RESULT_CANCELED)//回到选择照片来源
{
doPickPhotoAction();
return;
}
    if (resultCode != RESULT_OK){ //回到主页
     startActivity(new Intent(PicPostActivity.this,MainActivity.class));
     finish();
        return;  
    }
    switch (requestCode) {  
    case PHOTO_PICKED_WITH_DATA: {// 调用Gallery返回的
     break;
    }
        case CAMERA_WITH_DATA: {// 照相机程序返回的,再次调用图片剪辑程序去修剪图片 
            doCropPhoto(mCurrentPhotoFile);
            break;
        }
            case PHOTO_CUT_WITH_DATA:{
             if (data != null) {
             try {
setPicToView(data); //裁剪图片
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "裁剪失败!", Toast.LENGTH_LONG).show();
HengtuoUtil.log(3, TAG+".onActivityResult",  "裁剪失败!");
}
          }
             break;
            }
        }  
      
}  
  
private void setPicToView(Intent picdata) throws Exception {
Bundle extras = picdata.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
picName = getPhotoFileName();
File f = new File(Constants.PHOTO_DIR_TEMP);
if(!f.exists())
f.mkdirs();
mCurrentPhotoFile = new File(Constants.PHOTO_DIR_TEMP+"/"+ picName);
mCurrentPhotoFile.createNewFile();
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(mCurrentPhotoFile));
        photo.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        bos.flush();
        bos.close();
        HengtuoUtil.log(0, TAG, "图片保存成功");
        
        ImageView imgView = (ImageView) findViewById(R.id.img_view);// 获取控件
            imgView.setImageBitmap(photo);// 填充控件
}
}
protected void doCropPhoto(File f) {  
    try {  
        // 启动gallery去剪辑这个照片  
        final Intent intent = getCropImageIntent(Uri.fromFile(f));  
        startActivityForResult(intent, PHOTO_CUT_WITH_DATA);  
    } catch (Exception e) {  
        Toast.makeText(this, "没有找到照片", Toast.LENGTH_LONG).show();
        HengtuoUtil.log(3, TAG, getString(R.string.photoPickerNotFoundText));
    }  
}  
  
/**  
* Constructs an intent for image cropping. 调用图片剪辑程序  
*/  
public static Intent getCropImageIntent(Uri photoUri) {  
    Intent intent = new Intent("com.android.camera.action.CROP");  
    intent.setDataAndType(photoUri, "image/*");  
    intent.putExtra("crop", "true");  
    intent.putExtra("aspectX", 1);  
    intent.putExtra("aspectY", 1);  
    intent.putExtra("outputX", 600);  
    intent.putExtra("outputY", 400);  
    intent.putExtra("return-data", true);  
    return intent;  
}
android照片Gallery