问题::如果用户未上传任何图片,则应用崩溃,因为无法存储以前的图片...
概述:我的应用程序由“类别”和“编辑器”活动组成。
尝试在空对象引用上调用虚拟方法'java.lang.String android.net.Uri.toString()'
如果用户再次上传图片-相同或不同,没关系,则该应用程序运行良好。
目标是如果用户不想上传新图片,则保留图片,就像应用程序保留EditText字段的值一样:
private void saveInventory() {
// Read from input fields
// Use trim to eliminate leading or trailing white space
String nameString = mNameEditText.getText().toString().trim();
String infoString = mAdditionalInfoEditText.getText().toString().trim();
String priceString = mPriceEditText.getText().toString().trim();
String quantityString = mQuantityTextView.getText().toString().trim();
String image = actualUri.toString(); <---- error here
通过这种方法选择图像:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
// The ACTION_OPEN_DOCUMENT intent was sent with the request code READ_REQUEST_CODE.
// If the request code seen here doesn't match, it's the response to some other intent,
// and the below code shouldn't run at all.
if (requestCode == SELECT_AN_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {
// The document selected by the user won't be returned in the intent.
// Instead, a URI to that document will be contained in the return intent
// provided to this method as a parameter. Pull that uri using "resultData.getData()"
if (resultData != null) {
actualUri = resultData.getData();
mPhotoImageView.setImageURI(actualUri);
}
}
}
我相信保留活动状态并恢复其状态可以解决问题(下面的代码),但是我不知道将其粘贴到代码中的位置。...
// Save the activity state when it's going to stop.
// @Override
// protected void onSaveInstanceState(Bundle outState) {
// super.onSaveInstanceState(outState);
// outState.putParcelable("actualUri", actualUri);
// }
// Recover the saved state when the activity is recreated.
// @Override
// protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
// super.onRestoreInstanceState(savedInstanceState);
// actualUri= savedInstanceState.getParcelable("actualUri");
// }
可能有人知道保存编辑器活动后如何保留相同的图像。谢谢。
P.S。 finish活动退出编辑器,然后返回到Catalog。所有数据都保存在数据库中-除图像以外的。
@Override public boolean onOptionsItemSelected(MenuItem item) {
// User clicked on a menu option in the app bar overflow menu switch (item.getItemId())
{ // Respond to a click on the "Save" menu option case R.id.action_save:
// Save to database saveInventory();
// Exit activity finish(); return true;
答案 0 :(得分:0)
我已经解决了问题。
我必须删除导致“保存”类错误的行,并在该类中创建单独的验证:
if (actualUri== null) {
Toast.makeText(this, getString(R.string.image_required), Toast.LENGTH_SHORT).show();
return hasAllRequiredValues;
} else {
values.put(InventoryEntry.COLUMN_INVENTORY_IMAGE, actualUri.toString());
}