Код успешно запущен. Я хочу сохранить фотографию и показать в виде изображения. Этот код выполнил мою потребность. Я пытался фотографировать постоянно. Но это прекратилось после съемки одного снимка. Эти ошибки показаны как исключение во время выполнения, исходная съемка не удалась и т.д. c. Теперь, что я могу сделать? разрешение:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class CameraActivity extends AppCompatActivity {
private Camera camera;
private ShowCamera showCamera;
private ImageView imageView;
private Button button;
private static final int REQUEST_CAMERA_PERMISSION=200;
private final Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bp = BitmapFactory.decodeByteArray(data, 0, data.length);
File photo = new File(Environment.getExternalStorageDirectory(), "Photo.jpg");
Bitmap resized=Bitmap.createScaledBitmap(bp,(int)(bp.getWidth()*0.7),(int)(bp.getHeight()*0.7),true);
if (photo.exists()) {
photo.delete();
}
try {
FileOutputStream fos = new FileOutputStream(photo.getPath());
fos.write(data);
Toast.makeText(CameraActivity.this,"Saved"+photo.getPath(),Toast.LENGTH_LONG).show();
fos.close();
} catch (IOException e) {
Toast.makeText(CameraActivity.this," "+e.getMessage(),Toast.LENGTH_LONG).show();
}
imageView.setImageBitmap(resized);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
checkPermission();
camera=Camera.open();
imageView=(ImageView)findViewById(R.id.imgv);
showCamera=new ShowCamera(CameraActivity.this,camera);
FrameLayout preview=(FrameLayout)findViewById(R.id.framlayout);
preview.addView(showCamera);
button=(Button)findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
camera.takePicture(null,null, mPicture);
}
});
}
private void checkPermission() {
if(ActivityCompat.checkSelfPermission(CameraActivity.this,Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(CameraActivity.this,new String[] {
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE
},REQUEST_CAMERA_PERMISSION);
return;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode == REQUEST_CAMERA_PERMISSION)
{
if(grantResults[0] != PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(this, "You can't use camera without permission", Toast.LENGTH_SHORT).show();
finish();
}
}
}
}
ShowCamera. java
import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
import java.io.IOException;
import java.util.List;
public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mholder;
private Camera mcamera;
public ShowCamera(Context context, Camera camera) {
super(context);
mcamera=camera;
mholder=getHolder();
mholder.addCallback(this);
mholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Camera.Parameters params=mcamera.getParameters();
List<Camera.Size> sizes= params.getSupportedPictureSizes();
Camera.Size msize=sizes.get(0);
for(int i=0;i<sizes.size();i++)
{
if(sizes.get(i).width>msize.width)
{
msize=sizes.get(i);
}
}
if(ShowCamera.this.getResources().getConfiguration().orientation!= Configuration.ORIENTATION_LANDSCAPE)
{
mcamera.setDisplayOrientation(90);
params.setRotation(90);
}else
{
mcamera.setDisplayOrientation(0);
params.setRotation(0);
}
params.setPictureSize(msize.width,msize.height);
mcamera.setParameters(params);
try {
mcamera.setPreviewDisplay(holder);
mcamera.startPreview();
}catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if(mholder.getSurface()==null){
return;
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mcamera.stopPreview();
mcamera.release();
}
}