如何在拍照后在其他活动中显示图像?

时间:2017-08-14 15:29:59

标签: java android image camera display

我想要拍摄的照片并在另一项活动中展示。我最好希望有一个按钮用于下一个活动,当点击时,会显示我拍摄的图像。只需要一个简单的方法。我是android studio的新手,因此我不太清楚如何解决这个问题。

这是我的代码

Scanner.java

package mapp.com.sg.receiptscanner;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;



public class Scanner extends AppCompatActivity {

ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scanner);

    Button btnCamera = (Button)findViewById(R.id.btnCamera);
    imageView = (ImageView)findViewById(R.id.imageView);

    btnCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent,0);
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap bitmap = (Bitmap) data.getExtras().get("data");
    imageView.setImageBitmap(bitmap);


}

public void onClick(View view) {
    Intent i = new Intent(this, Info.class);
    startActivity(i);
}
}

activity_scanner.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="mapp.com.sg.receiptscanner.Scanner">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="9"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#1f4991"
    android:textColor="@android:color/white"
    android:text="Open Camera"
    android:id="@+id/btnCamera"
    android:layout_marginBottom="14dp"
    android:layout_above="@+id/ProceedBtn"
    android:layout_alignStart="@+id/ProceedBtn" />

<Button
    android:id="@+id/ProceedBtn"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Proceed"
    android:background="#1f4991"
    android:textColor="@android:color/white"
    android:onClick="onClick"/>

1 个答案:

答案 0 :(得分:2)

尝试将照片转换为字节并将其发送到另一个活动。

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image = stream.toByteArray();

Intent intent = new Intent(this, YourActivity.class);
intent.putExtra("photo", image);
startActivity(intent);

在下一个活动中进行此操作,并使用decodeByteArray创建位图并设置到图像视图中。

YourActivity.class

    byte[] byteArrayExtra = getIntent().getByteArrayExtra("photo");

    //BitmapOptions is optional you can create bitmap without this also. This is the description of its use from google developer docs.
    //BitmapFactory.Options: null-ok; Options that control downsampling and whether the image should be completely decoded, or just is size returned.

    Bitmap bitmap = BitmapFactory.decodeByteArray(byteArrayExtra, 0, byteArrayExtra.length, new BitmapFactory.Options());

    //or
    Bitmap bitmap = BitmapFactory.decodeByteArray(byteArrayExtra, 0, byteArrayExtra.length);


    imageview.setimageBitmap(bitmap);