1

我正在开发我的 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);
}

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

4

4 回答 4

1

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

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);
  }
}
于 2015-11-18T15:50:58.203 回答
1

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

或者由于您在第一个活动中已经获得了位图,您可以直接通过意图发送位图,如下所示。

https://stackoverflow.com/a/2459624/5577385

于 2015-11-18T15:57:46.163 回答
1

你可以这样做:

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

}

于 2015-11-26T04:30:38.257 回答
0

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

于 2015-11-18T15:52:12.247 回答