Android拍照软件的制作,解决了按钮调用相机闪退、存储问题和存储缓慢等问题

tech2022-07-15  148

Android拍照软件的制作,包括拍照,存储的功能,以下为MainActivity的内容 解决了按钮闪退、拍照照片存储缓慢等问题 加了裁剪功能但是使用还需要完善。 文件名是我的,需要修改,希望能帮助到和我一样问题的人。 如果还是需要整个文件来参考的话我的主页有上传整个安卓文件。

仅仅适用于安卓9以及安卓9以下

package com.example.takephoto; import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.FileProvider; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.MediaScannerConnection; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.os.StrictMode; import android.provider.MediaStore; import android.view.View; import android.widget.ImageView; import android.widget.Toast; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; public class MainActivity extends AppCompatActivity { private static final int ACTIVITY_START_CAMERA_APP=0; private ImageView mPhotoCapturedImageView; private String mImageFileLocation=""; // private String mImageFileLocation="/app"; // private String mImageFileLocation="/storage/DCIM/Camera/"; // private Uri imageLocation; // private String mDate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPhotoCapturedImageView=(ImageView) findViewById(R.id.capturePhotoImageView); initPhotoError(); } public void takePhoto (View view){ Toast.makeText(this,"照相按钮被点击",Toast.LENGTH_SHORT).show(); Intent callCameraAppIntent=new Intent(); callCameraAppIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); File photoFile=null; try{ photoFile=createImageFile(); }catch (IOException e){ e.printStackTrace(); } Uri uri=Uri.fromFile(photoFile); callCameraAppIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(callCameraAppIntent,ACTIVITY_START_CAMERA_APP ); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + mImageFileLocation))); } protected void onActivityResult(int requestCode,int resultCode,Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK) { Toast.makeText(this, "照片获取成功!", Toast.LENGTH_SHORT).show(); // Bundle extras=data.getExtras(); // Bitmap photoCapturedBitmap=(Bitmap) extras.get("data"); // mPhotoCapturedImageView.setImageBitmap(photoCapturedBitmap); Bitmap photoCapturedBitmap = BitmapFactory.decodeFile(mImageFileLocation); mPhotoCapturedImageView.setImageBitmap(photoCapturedBitmap); setReducedImageSize(); } } File createImageFile() throws IOException { // mFilePath = Environment.getExternalStorageDirectory().getPath(); String timeStamp= new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format(new Date()); String imageFileName="IMAGE-"+timeStamp+"-"; File storageDirectory= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); File image=File.createTempFile(imageFileName,".jpg",storageDirectory); mImageFileLocation=image.getAbsolutePath(); return image; } void setReducedImageSize(){ int targetImageWidth=mPhotoCapturedImageView.getWidth(); int targetImageHeight=mPhotoCapturedImageView.getHeight(); BitmapFactory.Options bmOptions=new BitmapFactory.Options(); bmOptions.inJustDecodeBounds=true; BitmapFactory.decodeFile(mImageFileLocation,bmOptions); int cameraImageWidth=bmOptions.outWidth; int cameraImageHeight=bmOptions.outHeight; int scaleFactor = Math.min(cameraImageWidth/targetImageWidth,cameraImageHeight/targetImageHeight); bmOptions.inSampleSize=scaleFactor; bmOptions.inJustDecodeBounds=false; Bitmap photoReducedSizeBitmap=BitmapFactory.decodeFile(mImageFileLocation,bmOptions); mPhotoCapturedImageView.setImageBitmap(photoReducedSizeBitmap); } //此代码为解决闪退问题代码 private void initPhotoError(){ StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); builder.detectFileUriExposure(); } }
最新回复(0)