Android调用系统照相机返回intent为空原因分析

1.在调用android系统相册时,使用的是如下方式:

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("image/");

    startActivityForResult(intent,1);

 在onActivityResult 中通过data获取数据可以正常获取:

if (requestCode == 1)
        {
            Uri uri = null;
            if (null != data || resultCode == RESULT_OK)
            {
                uri = data.getData();
                String filePath = getRealPathFromUri(this,uri);
                Log.d("filepath",filePath);
                if (null == filePath || filePath.equals(""))
                {
                    return;
                }
       
// To do something

       //得到了文件路径,可以执行相应压缩上传操作,      
            }
        }
        super.onActivityResult(requestCode, resultCode, data);

public static String getRealPathFromUri(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            if (null == cursor )
            {
                return "";
            }
            if (cursor.moveToFirst())
            {
                return cursor.getString(0);
            }
        }catch (Exception e)
        {
            Log.w("resolvepic",e.getMessage());
        }
        finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return "";
    }


2.回到正题,如何调用系统照相机呢?如下:


Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 指定存放拍摄照片的位置
File filePath = createImageFile();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(filePath));
startActivityForResult(intent, 2);
------------------------------------------------------我是分隔线------------------------------------------------------

private File createImageFile() throws IOException {

    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
    String timeStamp = format.format(System.currentTimeMillis());
    String imageFileName = "hxjysj_" + timeStamp + ".jpg";
    File image = new File(PictureUtil.getAlbumDir(this), imageFileName);
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;

在onActivityResult中:
protected void onActivityResult(int requestCode, int resultCode,
                                Intent intent) 
	其中intent得到的结果总是为null???
查看相关资料发现,如果指定intent.putExtra(MediaStore.EXTRA_OUT,uri);
则intent拿到的是null.  如果没有指定uri,则intent就会有数据的返回.
照相机会有自己的默认的存储路径,如果想得到图片的路径,则可以通过
Bitmap bitmap = (Bitmap)intent.getParcelableExtra("data");
得到bitmap再进行图片的压缩展示等相关的操作.
3.附Camera.java部分源码:
 private Bitmap createCaptureBitmap(byte[] data) {
        // This is really stupid...we just want to read the orientation in
        // the jpeg header.
        String filepath = ImageManager.getTempJpegPath();
        int degree = 0;
        if (saveDataToFile(filepath, data)) {
            degree = ImageManager.getExifOrientation(filepath);
            new File(filepath).delete();
        }

//缩放的位图bitmap只有50k,如果还想得到高质量的图片还是指定图片路径吧.
        // Limit to 50k pixels so we can return it in the intent.
        Bitmap bitmap = Util.makeBitmap(data, 50 * 1024);
        bitmap = Util.rotate(bitmap, degree);
        return bitmap;
    }
private void doAttach() {
        if (mPausing) {
            return;
        }


        byte[] data = mImageCapture.getLastCaptureData();


        if (mCropValue == null) {
            // First handle the no crop case -- just return the value.  If the
            // caller specifies a "save uri" then write the data to it's
            // stream. Otherwise, pass back a scaled down version of the bitmap
            // directly in the extras.
//如果指定uri就会保存到指定的uri中,否则,通过缩放位图的缩放版本
            if (mSaveUri != null) {  
//这里说明的就是设置了指定保存的uri地址
                OutputStream outputStream = null;
                try {
                    outputStream = mContentResolver.openOutputStream(mSaveUri);
                    outputStream.write(data);
                    outputStream.close();

			      //设置了成功的返回状态.
                    setResult(RESULT_OK);
                    finish();
                } catch (IOException ex) {
                    // ignore exception
                } finally {
                    Util.closeSilently(outputStream);
                }
            } else {
                Bitmap bitmap = createCaptureBitmap(data);
                setResult(RESULT_OK,  
                        new Intent("inline-data").putExtra("data", bitmap));
//如果没有设置指定的保存地址.intent就会有数据返回.
//在onActivityResult中可以通过如下方式得到一个bitmap对象再进行操作:
//Bitmap bitmap = (Bitmap)intent.getParcelableExtra("data");

                finish();
            }
        } else {
            // Save the image to a temp file and invoke the cropper
            Uri tempUri = null;
            FileOutputStream tempStream = null;
            try {
                File path = getFileStreamPath(sTempCropFilename);
                path.delete();
                tempStream = openFileOutput(sTempCropFilename, 0);
                tempStream.write(data);
                tempStream.close();
                tempUri = Uri.fromFile(path);
            } catch (FileNotFoundException ex) {
                setResult(Activity.RESULT_CANCELED);
                finish();
                return;
            } catch (IOException ex) {
                setResult(Activity.RESULT_CANCELED);
                finish();
                return;
            } finally {
                Util.closeSilently(tempStream);
            }


            Bundle newExtras = new Bundle();
            if (mCropValue.equals("circle")) {
                newExtras.putString("circleCrop", "true");
            }
            if (mSaveUri != null) {
                newExtras.putParcelable(MediaStore.EXTRA_OUTPUT, mSaveUri);
            } else {
                newExtras.putBoolean("return-data", true);
            }


            Intent cropIntent = new Intent("com.android.camera.action.CROP");


            cropIntent.setData(tempUri);
            cropIntent.putExtras(newExtras);


            startActivityForResult(cropIntent, CROP_MSG);
        }
    }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值