如何将图像传输到另一个活动

huangapple go评论80阅读模式
英文:

How to transfer image to another activity

问题

private void openCamera() {
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, "New Picture");
    values.put(MediaStore.Images.Media.DESCRIPTION, "Taking pic from the Camera");
    image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
    startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    SelectedImage.setImageURI(image_uri);
    try {
        Bitmap ImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), image_uri);
        detectImage(ImageBitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
英文:

I have two activity, First with button, which calls void openCamera, and second, where I need to get bitmap.<br>
I have two questions: <br>

  1. Which line of code is saving pictures? <br>
  2. How I can take a bitmap from OnActivityResult and get it in another activity?
private void openCamera() {
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, &quot;New Picture&quot;);
        values.put(MediaStore.Images.Media.DESCRIPTION, &quot;Taking pic from the Camera&quot;);
        image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
        startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        SelectedImage.setImageURI(image_uri);
        try {
            Bitmap ImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), image_uri);
            detectImage(ImageBitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

答案1

得分: 1

不要考虑通过 Bundle 进行传递(文档:https://developer.android.com/guide/topics/resources/runtime-changes.html#RetainingAnObject)

我认为,你应该将图像保存在应用的内部存储中,然后在第二个活动中加载它。
以下是解释如何做到这一点的答案:https://stackoverflow.com/questions/17674634/saving-and-reading-bitmaps-images-from-internal-memory-in-android

英文:

Don't think about passing it through Bundle (docs: https://developer.android.com/guide/topics/resources/runtime-changes.html#RetainingAnObject)

I think, you should save the image in internal storage of your app and then load it in your second activity.
Here are the answers explaining how to do that: https://stackoverflow.com/questions/17674634/saving-and-reading-bitmaps-images-from-internal-memory-in-android

答案2

得分: 0

将图像URI作为字符串与活动意图一起传递

mIntent.putExtra("image", image_url.toString());

在接收活动中获取URI并将代码移动到生成位图的位置。

String image_url = getIntent().getStringExtra("image");
//获取位图的代码

英文:

Pass image uri as string along with activity intent

mIntent.putExtra(&quot;image&quot;, image_url.toString());

In the receiving activity get the uri and move your code to generate bitmap there.

String image_url = getIntent().getStringExtra(&quot;image&quot;);
//your code to get bitmap

huangapple
  • 本文由 发表于 2020年5月2日 18:44:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/61558063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定