我想要我拍摄的照片并在另一个活动中展示。我最好希望有一个用于下一个活动的按钮,单击时会显示我拍摄的图像。只需要一个简单的方法。我是安卓工作室的新手,因此我不太确定如何解决这个问题。
这是我的代码
扫描仪.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"/>
尝试将照片转换为字节并将其发送到其他活动。
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);