所以我今天一直在处理这段代码,我似乎无法弄清楚导致这个ParseException抛出的原因。有时确实如此,有时却没有。似乎导致抛出的是当每个字段中有更多数据要上传到Parse时。当数据最少时,该项目会保存其照片并且没有问题。我读到可能是因为我没有在将它添加到对象之前设置ParseFile,但是我和我尝试切换代码无效。可能导致它的任何想法?
public class AddItemActivity extends AppCompatActivity {
private EditText mItemName;
private String mItemNameString;
private EditText mItemDescription;
private String mItemDescriptionString;
private EditText mItemPrice;
private String mItemPriceString;
private Button mAddItemButton;
private Date mAddDate;
private String mAddDateString;
private boolean mItemSoldBoolean;
private ImageView mDeleteButton;
private ImageView mBackButton;
private ImageView mAddImageIcon;
private TextView mAddImageLabel;
private Bitmap mItemImageBitmap;
private byte[] mItemImageByteArray;
private ProgressBar mUploadProgress;
private ParseObject mNewItem;
private ParseFile mItemImageFile;
private boolean mCameraUsed = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
mUploadProgress = (ProgressBar) findViewById(R.id.uploadProgress);
mUploadProgress.setVisibility(View.INVISIBLE);
mDeleteButton = (ImageView) findViewById(R.id.deleteButton);
mDeleteButton.setVisibility(View.INVISIBLE);
mAddImageIcon = (ImageView) findViewById(R.id.addImageIcon);
mAddImageLabel = (TextView) findViewById(R.id.addItemLabel);
mBackButton = (ImageView) findViewById(R.id.backButton);
mBackButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goBack();
}
});
mAddImageIcon = (ImageView) findViewById(R.id.addImageIcon);
mAddImageIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takePhoto();
}
});
mAddItemButton = (Button) findViewById(R.id.submitItemButton);
mAddItemButton.setVisibility(View.VISIBLE);
mAddItemButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mCameraUsed) {
saveItemToInventory();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(AddItemActivity.this);
builder.setTitle("THERE'S NO PHOTO!")
.setMessage("You haven't added an photo of your item! Would you like to add one now, or continue and add one later?")
.setPositiveButton("TAKE PHOTO NOW", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
takePhoto();
}
})
.setNegativeButton("CONTINUE WITHOUT PHOTO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
runOnUiThread(new Runnable() {
@Override
public void run() {
saveItemToInventory();
mCameraUsed = false;
}
});
}
});
builder.show();
}
}
});
}
private void takePhoto() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 4);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && data != null){
switch(requestCode) {
case 4:
Bundle photo = data.getExtras();
mItemImageBitmap = (Bitmap) photo.get("data");
if (mItemImageBitmap != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mItemImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
mItemImageByteArray = stream.toByteArray();
}
mAddImageIcon.setImageBitmap(mItemImageBitmap);
mAddImageLabel.setVisibility(View.INVISIBLE);
mCameraUsed = true;
}
}
}
private void goBack() {
Intent intent = new Intent(AddItemActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
@Override
public void finish() {
Intent intent = new Intent(AddItemActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
setResult(RESULT_OK);
startActivity(intent);
}
private void saveItemToInventory() {
mAddItemButton.setVisibility(View.INVISIBLE);
mUploadProgress.setVisibility(View.VISIBLE);
mItemName = (EditText) findViewById(R.id.itemNameField);
mItemNameString = mItemName.getText().toString().trim();
mItemDescription = (EditText) findViewById(R.id.itemDescriptionField);
mItemDescriptionString = mItemDescription.getText().toString().trim();
mItemPrice = (EditText) findViewById(R.id.itemPriceField);
mItemPriceString = mItemPrice.getText().toString().trim();
mAddDate = new Date();
mAddDateString = mAddDate.toString();
mItemSoldBoolean = false;
if (!mCameraUsed){
mItemImageBitmap = ((BitmapDrawable)mAddImageIcon.getDrawable()).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mItemImageBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
mItemImageByteArray = stream.toByteArray();
mItemImageFile = new ParseFile(mItemNameString + ".png", mItemImageByteArray);
mItemImageFile.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null){
createObject();
} else {
Toast.makeText(AddItemActivity.this, "There was an error. " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
} else {
mItemImageFile = new ParseFile(mItemNameString + ".jpg", mItemImageByteArray);
mItemImageFile.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
createObject();
} else {
Toast.makeText(AddItemActivity.this, "There was an error. " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
}
private void createObject() {
mNewItem = new ParseObject("Item");
mNewItem.put("name", mItemNameString);
mNewItem.put("description", mItemDescriptionString);
mNewItem.put("addDate", mAddDateString);
mNewItem.put("price", mItemPriceString);
mNewItem.put("itemSold", mItemSoldBoolean);
mNewItem.put("itemImage", mItemImageFile);
mNewItem.setACL(new ParseACL(ParseUser.getCurrentUser()));
mNewItem.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
mUploadProgress.setVisibility(View.INVISIBLE);
finish();
} else {
e.printStackTrace();
}
}
});
}
}
答案 0 :(得分:0)
在asynctask中解析文件并在asynctask的onPostExecute上进行编码