我在android中做了一个照片编辑器应用程序我有两个图像按钮(一个用于相机,另一个用于图库)。当我用相机拍摄照片或从图库中选择照片时,我希望照片显示在图像视图上的另一个活动中。我刚刚为相机编写了一些代码,但它没有用。如果有人可以帮助我,我将非常感激。我是编程的新手。
的manifest.xml
<manifest ..>
<uses-feature android:name = "android.hardware.camera" android:required="false"/>
第一项活动:
public class MainActivity extends Activity {
private static int IMG_RESULT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton take_photo = (ImageButton) findViewById(R.id.cameraButton);
ImageButton get_photo = (ImageButton) findViewById(R.id.galleryButton);
take_photo.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) {
if(requestCode == 0 && resultCode == RESULT_OK)
{
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
Intent intent = new Intent(this,ShowPhotoActivity.class);
intent.putExtra("BitmapImage",bitmap);`
startActivity(intent);`
第二项活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_photo);
ImageView showPhoto = (ImageView) findViewById(R.id.imageView);
Bitmap bitImage = getIntent().getParcelableExtra("BitmapImage");
showPhoto.setImageBitmap(bitImage);
答案 0 :(得分:0)
代码经过测试并正常运行。
在清单文件
上添加此项<uses-feature android:name="android.hardware.camera"></uses-feature>
第一项活动
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0 && resultCode == RESULT_OK){
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
Intent intent = new Intent(this,ShowPhotoActivity.class);
intent.putExtra("BitmapImage",bitmap);
startActivity(intent);
}
}
第二项活动
ImageView showPhoto = (ImageView) findViewById(R.id.imageView);
Bitmap bitImage = getIntent().getParcelableExtra("BitmapImage");
showPhoto.setImageBitmap(bitImage);
XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:adjustViewBounds="true"
android:layout_width="match_parent"
android:layout_height="300dp" />
</LinearLayout>