无法通过意图

时间:2015-11-23 10:17:11

标签: android android-intent android-camera uri android-camera-intent

我不确定我做错了什么。我已经尝试了很多来修复此代码,但无济于事。基本上用户点击活动A上的两个图像。图像称为medicine_bill和other_bill。通过意图活动B为每个图像传递Uri.getPath()。但是在活动B中,我为两个图像获取相同的Uri,因此无法加载它们。缩放图像是减少图像尺寸的函数。

    btnAddMedicineBill.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            File f=getOutputMediaFile("Medit_medicine");
            medicineBillUri = Uri.fromFile(f);
            Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT, medicineBillUri);
            startActivityForResult(i, BILL_IMAGE_MEDICINE);
        }
    });

    btnAddOtherBill.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {               
            File f=getOutputMediaFile("Medit_Other");
            otherBillUri = Uri.fromFile(f);
            Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT, otherBillUri);
            startActivityForResult(i, BILL_IMAGE_OTHER);
        }
    });

保存图像的路径生成器

private File getOutputMediaFile(String name) {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DCIM), "medit_app");
        if (! mediaStorageDir.exists()){
            mediaStorageDir.mkdirs();
        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                name+timeStamp + ".jpg");
        Log.d("reached a", mediaFile.toString());
        return mediaFile;
    }

onActivityResult()代码如下 -

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(String.valueOf(requestCode), String.valueOf(resultCode));

        if (requestCode==BILL_IMAGE_MEDICINE){
            if (resultCode==RESULT_OK) {
                Bitmap bitmap = ScaleImage(medicineBillUri.getPath());
                ivMedicineBillImage.setImageBitmap(bitmap);
                _medicineImageTaken =true;
                }
        }
        if (requestCode==BILL_IMAGE_OTHER){
            if (resultCode==RESULT_OK) {
                Bitmap bitmap1 = ScaleImage(otherBillUri.getPath());
                ivOtherBillImage.setImageBitmap(bitmap1);
                _otherImageTaken =true;
            }
        }
    }

活动B的startActivity()

Intent intentToStamp = new Intent(ActivityA.this, ActivityB.class);
intentToStamp.putExtra(BILL_OTHER_IMAGE_URL, otherBillUri.getPath());
intentToStamp.putExtra(BILL_MEDICINE_IMAGE_URL, medicineBillUri.getPath());
Log.d("Other Bill",otherBillUri.getPath() );
Log.d("Medicine Bill",medicineBillUri.getPath() );

此时的日志值

Other Bill﹕ /storage/emulated/0/DCIM/medit_app/Medit_Other20151123_152716.jpg
Medicine Bill﹕ /storage/emulated/0/DCIM/medit_app/Medit_medicine20151123_152707.jpg

最后是Activity B中的getExtras()代码

Intent intent=getIntent();
medicineBillImageUrl =intent.getStringExtra(BILL_MEDICINE_IMAGE_URL);
Log.d("Medicine", medicineBillImageUrl)
otherBillImageUri =intent.getStringExtra(BILL_OTHER_IMAGE_URL);
Log.d("Other", otherBillImageUri);

此时两个日志都显示为

Medicine Bill﹕ /storage/emulated/0/DCIM/medit_app/Medit_medicine20151123_152707.jpg
Other Bill﹕ /storage/emulated/0/DCIM/medit_app/Medit_medicine20151123_152707.jpg

实际图像也存储为/storage/emulated/0/DCIM/Camera/20151123_152707.jpg

1 个答案:

答案 0 :(得分:0)

您需要为

定义不同的键
i.putExtra(MediaStore.EXTRA_OTHER_BILL, otherBillUri);

i.putExtra(MediaStore.EXTRA_MEDICINE_BILL, medicineBillUri);