gpt4 book ai didi

java - 手机应用程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 10:39:34 25 4
gpt4 key购买 nike

我正在尝试为 Android 手机制作一个相机应用程序。代码没有显示错误,但是当我启动应用程序后在手机上运行它时,它崩溃了,并给出了以下错误:

"Unfortunately the app stopped"

package com.my.hp.myapp;

import android.Manifest;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CameraActivity extends AppCompatActivity implements View.OnClickListener {
private static final int REQUEST_CODE_SOME_FEATURES_PERMISSIONS = 1;
private static final int CAMERA_PIC_REQUEST = 2;
private ImageView img;
private Button btn1;
private Uri fileUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
img = findViewById(R.id.image);
btn1 = findViewById(R.id.butt);
btn1.setOnClickListener(this);
}

@RequiresApi(api = Build.VERSION_CODES.M)
private void checkpermission() {
int hasCameraPermission =
checkSelfPermission(Manifest.permission.CAMERA);
int hasStoragePermission =
checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
List<String> permissions = new ArrayList<>();
if (hasCameraPermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.CAMERA);
}
if (hasStoragePermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (!permissions.isEmpty()) {
requestPermissions(permissions.toArray(new String[permissions.size()]),
REQUEST_CODE_SOME_FEATURES_PERMISSIONS);
}
}

@RequiresApi(api=Build.VERSION_CODES.M)
private boolean hasPermission() {
int hasCameraPermission =
checkSelfPermission(Manifest.permission.CAMERA);
int hasStoragePermission =
checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);

return (hasCameraPermission==PackageManager.PERMISSION_GRANTED && hasStoragePermission==PackageManager.PERMISSION_GRANTED);
}

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(View view) {
switch(view.getId())
{
case R.id.butt:
if(hasPermission())
{
openCamera();
}
else{
checkpermission();
}
break;
}

}
private void openCamera()
{
String timeStamp=new
SimpleDateFormat("yyyyMMdd_HHmms").format(new Date());
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE,"IMG_"+ timeStamp+".jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri =
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
switch (requestCode) {
case REQUEST_CODE_SOME_FEATURES_PERMISSIONS: {
for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
openCamera();
} else if (grantResults[i] == PackageManager.PERMISSION_DENIED)
{
Log.d("Permissions", "Permission Denied: " + permissions[i]);
}
}
}
break;
default:{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
if (resultCode == RESULT_OK) {
if(data != null){
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
img.setImageBitmap(imageBitmap);
}
}
}
}
}

最佳答案

First check if you added camera and read storage permissions in AndroidManifest.xml

 <uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

关于java - 手机应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49139956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com