用相机拍照并将路径发送给其他人activity

Take picture with camera and send the path to other activity

我正在 Android 开发我的应用程序,但遇到了一个小问题。在我的 MainActivity 上,我拍了一张照片,然后将路径保存在一个字符串中,然后我将这个字符串发送到 CameraActivity。这是我的代码:

btnCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(cameraIntent, CAMERA_REQUEST);

            }
        });

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {

        Bitmap photo = (Bitmap) data.getExtras().get("data");

        Uri tempUri = getImageUri(getApplicationContext(), photo);

        String ruta = getRealPathFromURI(tempUri);
        Intent i = new Intent(getApplicationContext(), WarikeActivity.class);
        i.putExtra("ruta",ruta);
        startActivity(i);
    }

}

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
}

然后,在 CameraActivity 上我收到图片的路径,我正在尝试将图片放在我的 ImageView 上

void events(){
    Bundle extras = getIntent().getExtras();
    ruta = extras.getString("ruta");
    Bitmap imageBitmap = (Bitmap) extras.get(ruta);
    imgWarike.setImageBitmap(imageBitmap);
}

但是位图为空。知道为什么会这样吗?提前致谢。

不是拍照然后获取路径,而是先选择您想要的路径,然后将图像保存在那里。看看下面的代码:

private File file;
private URI imageUri;

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    String name = String.valueOf(System.currentTimeMillis() + ".jpg");
    file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    if (!file.exists()) {
        file.mkdirs();
    }
    File photo = new File(file, name);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, CAMERA_REQUEST);

@Override
protected void onActivityResult(int requestCode, int resultCode,    Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
    imageUri = data.getData();
    Intent i = new Intent(getApplicationContext(), WarikeActivity.class);
    i.putExtra("ruta", file);
    startActivity(i);
  }
}

您是否尝试遵循有关它的官方 Android 文档? http://developer.android.com/training/camera/photobasics.html

如果您试图从从 Intent 获得的路径中获取位图,请使用此代码。 Bitmap myBitmap = BitmapFactory.decodeFile(<yourFilepath>);

或者你在第一个activity中已经获取了位图,你直接通过intent发送位图如下。

你可以这样做:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
    Bitmap photo = (Bitmap) data.getExtras().get("data");

    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    Intent i = new Intent(getApplicationContext(), WarikeActivity.class);
    i.putExtra("foto", byteArray);
    startActivity(i);
}

然后:

void events(){
byte[] byteArray = getIntent().getByteArrayExtra("foto");
Bitmap foto = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
imgFoto.setImageBitmap(foto);

}