经过几天与这个问题的斗争,我发布了这个问题。我有浮动操作按钮和 onClick 侦听器调用 takePhoto() 方法,显然我想在其中拍摄照片并保存其位图,稍后我将使用该位图将其保存在数据库中。但这不是主要问题。我开始 onActivityResult (...) 来获取拍摄照片的数据。当我拍照并单击保存时,活动被简单地破坏了(我使用 Log.v (...) 检查它是否真的被破坏了)我几乎尝试了所有方法,包括覆盖 onSaveInstanceState (...) 等等。这是我的 Android 清单文件和部分代码。
显现:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alexparunov.collegemascots" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".account.SignIn"
android:label="@string/title_activity_login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".account.SignUp"
android:label="@string/title_activity_register"/>
<activity
android:name=".profile.MainProfile"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:noHistory="true"/>
</application>
Java:私有静态最终整数 REQUEST_IMAGE_CAPTURE = 1;
private void takePhoto(){
if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA},1);
}
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(cameraIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
Image finalImage = new Image();
if(user != null && imageBitmap != null) {
finalImage.setImageOwner(user.getUsername());
finalImage.setImageName(generateName());
finalImage.setImage(ImageUtils.getBytesFromBitmap(imageBitmap));
try {
ImageDatabase imageDatabase = new ImageDatabase(this);
imageDatabase.open();
finalImage.setImageId(imageDatabase.createImage(finalImage));
Log.v("TakeImage",""+finalImage.getImageId());
imageDatabase.close();
images.add(finalImage);
user.setImages(images);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
}
}
private String generateName(){
String name = user.getName()+"_";
if(images != null) {
name += images.size() + ".png";
}
return name;
}