user Intent将图像路径发送到另一个“活动”



有时我在手机上运行应用程序。问题是当我在相机拾取照片后单击"确定"按钮时,此时此刻!应用程序'停止运行!事实上,我想在另一个活动中看到照片!是否

拍照:

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()是否设置为null

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-启动相机以拍摄图像

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);

最新更新