所以我是第一次从事Android Studio工作,我有一个非常重要的项目。我正在尝试制作一个屏幕,上面显示一个带有顶部按钮的相机(如手套ui),如下所示:
现在我正在编写以下代码:
package com.none.www.aarogya;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.Image;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class HomeScreen extends AppCompatActivity {
public static final int CAMERA_REQUEST = 10;
private ImageView imgDumpCam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
imgDumpCam = findViewById(R.id.imgDumpCam);
btnTake = h(View v); {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//If user choose okay than following code will work
if(resultCode == RESULT_OK){
if(requestCode == CAMERA_REQUEST){
//we are hearing back from camera
Bitmap cameraImage = (Bitmap) data.getExtras().get("data");
// we get the image from the camera
imgDumpCam .setImageBitmap(cameraImage);
}
}
}
}
}
我遇到以下错误:
Error:(24, 9) error: illegal start of expression
Error:(24, 16) error: illegal start of expression
Error:(24, 35) error: ')' expected
Error:(24, 37) error: illegal start of expression
Error:(24, 38) error: ';' expected
Error:(46, 1) error: class, interface, or enum expected
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
您能帮我解决错误吗?
答案 0 :(得分:0)
btnTake = h(View v);
这句话是什么意思?如果要在btnTake上单击以拍摄照片,请执行以下操作
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 HomeScreen extends AppCompatActivity {
public static final int CAMERA_REQUEST = 10;
private ImageView imgDumpCam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
imgDumpCam = findViewById(R.id.imgDumpCam);
Button btnTake = findViewById(R.id.your_btn_take_id);
btnTake.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//If user choose okay than following code will work
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_REQUEST) {
//we are hearing back from camera
Bitmap cameraImage = (Bitmap) data.getExtras().get("data");
// we get the image from the camera
imgDumpCam.setImageBitmap(cameraImage);
}
}
}
}
答案 1 :(得分:0)
尝试使用此行代码从相机获取图像
首先在menifest.xml中添加摄像机权限 并检查运行时权限
然后添加此代码
login=findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickImageFromCamera();
}
});
}
public void clickImageFromCamera() {
Intent camIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (camIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(camIntent, REQUEST_CAMERA);
}
}
/*on activity result */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
if (data != null && data.getExtras() != null) {
}
}
}
我有此代码适用于。如果您在代码中遇到任何问题,请评论我