我正在尝试构建一个Android应用程序,该应用程序将使用Android相机意图拍摄照片,然后将其保存到图库中。我正在使用移动设备运行该应用程序(API21)。单击“捕获”按钮后,该应用程序正在启动并且相机正在启动。但是当我拍照时,它会显示它的快照,然后应用程序崩溃,并且图像没有保存在图库中。但是,当我导航到“设备文件资源管理器”中的“图片”文件夹时,可以找到其缩略图。有时,如果我重新启动手机,图像会显示在图库中。日志显示NullPointerException错误。
请注意,我也定位可能具有较低API级别的设备。 我们非常感谢您的帮助和支持。
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button btnCapture, btnCrop, btnAnalyze;
TextView tvResult;
ImageView Image;
final int CAMERA_PIC_REQUEST = 1; //can be any number
final int PERMISSION_REQUEST = 2;
String pathToFile;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCapture = findViewById(R.id.btnCapture);
btnCrop = findViewById(R.id.btnCrop);
btnAnalyze = findViewById(R.id.btnAnalyze);
tvResult = findViewById(R.id.tvResult);
btnCapture.setOnClickListener(new View.OnClickListener() {
//@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
takePictureAction();
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
takePictureAction();
} else {
Toast.makeText(this, "Cannot open camera", Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == CAMERA_PIC_REQUEST) {
Uri imageUri = data.getData();
InputStream inputStream;
try {
inputStream = getContentResolver().openInputStream(imageUri);
Bitmap image = BitmapFactory.decodeStream(inputStream);
Image.setImageBitmap(image);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Unable to open image", Toast.LENGTH_LONG).show();
}
}
}
}
private void takePictureAction() {
Uri pictureUri = FileProvider.getUriForFile(this,getApplicationContext().getPackageName() + ".provider", createPhotoFile());
Intent takepic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takepic.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
takepic.setFlags(Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
startActivityForResult(takepic, CAMERA_PIC_REQUEST);
Toast.makeText(MainActivity.this, "Camera launched!", Toast.LENGTH_SHORT).show();
galleryAddPic();
}
private File createPhotoFile() {
String name = new SimpleDateFormat("yyyyMMdd_HH_mm_ss").format(new Date());
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = null;
image = new File(storageDir, "picture" + name + ".jpg");
pathToFile = image.getAbsolutePath();
return image;
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(pathToFile);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.omm.mycamera">
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<provider
android:authorities="com.example.omm.mycamera.provider"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>