如何在新活动中打开拍摄的照片?

  • 本文关键字:照片 活动中 java android
  • 更新时间 :
  • 英文 :


我编码,当你点击按钮时,相机打开并拍摄一张新照片。我希望该图片在新活动上变成图像视图。因此,我创建了新活动并在其上放置了一个 ImageView:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.amy.teacherfilesapp.Upload">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:scaleType="centerCrop"
/>

</android.support.constraint.ConstraintLayout>

然后在我放的主要活动上(这整个事情适用于btn2所以你可以忽略btn1和btn3,谢谢(:'package com.example.amy.teacherfilesapp;

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

public class MainActivity extends AppCompatActivity {
Button btn1;
Button btn2;
Button btn3;
ImageView imgTakenPic;
private static final int CAM_REQUEST=1313;

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

btn2 = (Button) findViewById(R.id.drawer_two);
imgTakenPic = (ImageView)findViewById(R.id.imageView);
btn2.setOnClickListener(new btnTakePhotoClicker());
btn1 = (Button)findViewById(R.id.drawer_one);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent openCabinet = new Intent(MainActivity.this,MyCabinet.class);
startActivity(openCabinet);
}
});
btn2 =(Button)findViewById(R.id.drawer_two);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent upload = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(upload);
}
});
btn3 = (Button)findViewById(R.id.drawer_three);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent settings = new Intent(MainActivity.this, Settings.class);
startActivity(settings);
}
});


}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAM_REQUEST){
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imgTakenPic.setImageBitmap(bitmap);
}
}
class btnTakePhotoClicker implements  Button.OnClickListener{
@Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CAM_REQUEST);
}
}
}

'

这不起作用,因为它没有在我能看到的任何地方显示图像。

如果您能帮助我,我将不胜感激。谢谢。

像这样使用;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAM_REQUEST){
Bitmap photo = (Bitmap) data.getExtras().get("data");
/* Passing BITMAP to the Second Activity */
Intent IntentCamera = new Intent(this, Second.class);
IntentCamera.putExtra("BitmapImage", photo);
startActivity(IntentCamera);
}
}

在第二项活动中;

Intent intent_camera = getIntent();
Bitmap camera_img_bitmap = (Bitmap) intent_camera
.getParcelableExtra("BitmapImage");
if (camera_img_bitmap != null) {
img_to_be_zoomed.setImageBitmap(camera_img_bitmap);
}

最新更新