【问题标题】:How do I take a picture from camera and display it fullscreen in the next activity for editing?如何从相机拍摄照片并在下一个活动中全屏显示以进行编辑?
【发布时间】:2013-06-19 05:28:28
【问题描述】:

在我的应用中,我想让用户拍照或从他们的库中选择要编辑的图片。

我的MainActivity:

public void openCamera(View view)
{
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  
}

public void openLibrary(View view)
{
    Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, LIBRARY_PIC_REQUEST);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    Uri path = data.getData();

    Intent intent = new Intent(this, StickerActivity.class);
    intent.putExtra(EXTRA_MESSAGE, path);       
    startActivity(intent);
}

我将 URI 传递给我的下一个 Activity,这让用户可以选择贴在所选图片上的贴纸。从那里,URI 再次传递给EditingActivity。

这就是我的EditingActivity 的onCreate() 方法中的内容:

Bundle retrievedExtras = getIntent().getExtras();
    int stickerType = retrievedExtras.getInt(StickerActivity.SELECTED_MESSAGE);
    Uri picturePath = retrievedExtras.getParcelable(MainActivity.EXTRA_MESSAGE);

    paint = new Paint();
    boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    int frameBufferWidth = isPortrait ? 800 : 1280;
    int frameBufferHeight = isPortrait ? 1280 : 800;
    Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
            frameBufferHeight, Config.RGB_565);

    try
    {
        chosenPicture = Media.getBitmap(this.getContentResolver(), picturePath);
    } catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    } catch (IOException e) 
    {
        e.printStackTrace();
    }

    canvas = new Canvas(frameBuffer);
    canvas.drawBitmap(chosenPicture, 0, 0, paint);

现在,到目前为止,我只使用ImageView 显示了图片,但是我希望用户能够触摸他们想要在图片上粘贴的位置,并将编辑后的图片保存在原始图片上图片。现在我无法让原始图片在我的EditingActivity 中以位图的形式全屏显示。

我对 Android 还很陌生,我在这里检查了许多类似的问题,这些问题似乎几乎是我的解决方案,但并不完全。

简而言之我的问题:

如何从 URI 中获取位图?如果我实际上是从上面的代码中获取位图,为什么它不绘制(我是否必须覆盖 onDraw() 方法才能使用画布)?另外,当我可以在原始图片上放置贴纸时,如何将新图像(带有贴纸)保存到设备中?

【问题讨论】:

标签: android eclipse graphics bitmap image


【解决方案1】:

试试下面的代码

Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);

编辑

要获取URI,请使用以下代码。

Uri path = data.getExtras().get("data"); 

【讨论】:

  • 我编辑了我的答案。如果我猜的话,你没有选择正确的“URI”。只需更改该代码,然后告诉我。
  • 您是否尝试在第一个和第二个活动中打印您的 URI?是一样的,有区别吗?还是正确的?
  • 我在第二个活动中打印了URI;我会确保在早上的两个活动中检查它是否正确 - 另外,我认为我在第二个代码块的开头有相同的代码?我可能是错的,但它的功能与您的“Uri path = data.getExtras().get("data"); ”片段不一样吗?
  • 嗨 Chintan,我确保通过这两个活动的 URI 是相同的,它是;它显示为“内容://媒体/外部/图像/媒体/(数字)”
猜你喜欢
  • 1970-01-01
  • 2016-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多