user Intent将图像路径发送到另一个Activity

时间:2013-11-27 11:16:53

标签: android android-intent

有时我在手机上运行App.problem是我在相机拍照后点击确定按钮,此时此刻!应用'停止运行!事实上,我想在另一个活动上看到Pic!是

拍照:

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 = getActivity().getContentResolver().insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
        startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            Toast.makeText(getActivity(), R.string.take_photo_rem,
                    Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(getActivity(), 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);
}

onActivityResult:用户意图发送图片uri

public 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(getActivity(), R.string.photo_err,
                    Toast.LENGTH_LONG).show();
            return;
        }
        photoUri = data.getData();
        if (photoUri == null) {
            Toast.makeText(getActivity(), R.string.photo_err,
                    Toast.LENGTH_LONG).show();
            return;
        }
    }
    String[] pojo = { MediaStore.Images.Media.DATA };
    Cursor cursor = getActivity().managedQuery(photoUri, pojo, null, null,
            null);
    if (cursor != null) {
        int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
        cursor.moveToFirst();
        picPath = cursor.getString(columnIndex);
        try {
            if (Integer.parseInt(Build.VERSION.SDK) < 14) {

                cursor.close();
            }
        } catch (Exception e) {
            Log.e(TAG, "error:" + e);
        }
    }
    Log.i(TAG, "imagePath = " + picPath);
    if (picPath != null) {

        Intent startEx = new Intent(getActivity(), PhotoPre.class);
        Bundle bundle = new Bundle();
        bundle.putString(SAVED_IMAGE_DIR_PATH, picPath);
        startEx.putExtras(bundle);
        startActivity(startEx);

    } else {
        Toast.makeText(getActivity(), R.string.photo_err, Toast.LENGTH_LONG)
                .show();

    }

}

预览图片活动!是getIntent()是否为空?

Bundle bundle = getIntent().getExtras();
    picPath = bundle.getString(KEY_PHOTO_PATH);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    bm = BitmapFactory.decodeFile(picPath, options);

1 个答案:

答案 0 :(得分:0)

1 - 启动摄像头拍摄图像

Intent cameraIntent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment .getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, Const.dbSrNo + "image.jpg");
Uri uriSavedImage = Uri.fromFile(image);   
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); 
startActivityForResult(cameraIntent, CAMERA_REQUEST);

2 - 在“onActivityResult”中写下以下代码

if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
    File imgFile = new File(Environment.getExternalStorageDirectory(),
            "/MyImages/");

    /*photo = BitmapFactory.decodeFile(imgFile.getAbsolutePath() + "/"
            + Const.dbSrNo + "image.jpg");*/

    BitmapFactory.Options options = new BitmapFactory.Options();

    options.inSampleSize = 4;
    options.inPurgeable=true;
    Bitmap bm = BitmapFactory.decodeFile(imgFile.getAbsolutePath() + "/"
            + Const.dbSrNo + "image.jpg",options);

    imageView2.setImageBitmap(bm);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object 

    byteImage_photo = baos.toByteArray(); 

    Const.imgbyte=byteImage_photo;  

3 - 生成一个java文件Const.java

public class Const {
    public static byte[] imgbyte = "";
}  

4 - 现在使用

在您的活动中显示该图像
byte[] mybits=Const.imgbyte;
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeByteArray(mybits, 0, mybits.length, options);
yourImageview.setImageBitmap(bitmap);