我的应用程序半个月前运行良好,但现在我有问题。 我无法在某些表格中将图片上传到parse.com。它仅在设备上有问题,在模拟器应用程序上运行良好。 我正在尝试这段代码(从图库中选择图片):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null) {
String realPath;
// SDK < API11
if (Build.VERSION.SDK_INT < 11)
realPath = RealPathUtil.getRealPathFromURI_BelowAPI11(this, data.getData());
// SDK >= 11 && SDK < 19
else if (Build.VERSION.SDK_INT < 19)
realPath = RealPathUtil.getRealPathFromURI_API11to18(this, data.getData());
// SDK > 19 (Android 4.4)
else
realPath = RealPathUtil.getRealPathFromURI_API19(this, data.getData());
File file = new File(realPath);
try {
byte bytes[] = new byte[(int) file.length()];
BufferedInputStream bf = new BufferedInputStream(new FileInputStream(file));
bf.read(bytes, 0, bytes.length);
bf.close();
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
((ImageView) findViewById(R.id.setIMG)).setImageBitmap(bmp);
pFile = new ParseFile("file", bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
之后,当用户点击特殊按钮时,执行以下代码:
void sendPost(String txt) throws ParseException {
String parseUserId = getIntent().getExtras().getString(ObjConst.OBJECT_ID);
ParseUser parseUser = null;
ParseQuery<ParseUser> queryUser = ParseUser.getQuery();
queryUser.whereEqualTo(ObjConst.OBJECT_ID, parseUserId);
try {
parseUser = queryUser.getFirst();
} catch (ParseException e) {
Log.e("INTERNET", "Problem with internet connection or can`t find your accaunt.");
}
if (parseUser != null) {
final ParseObject parseObject = new ParseObject(ObjConst.POST);
parseObject.put(ObjConst.POST_ANONIMOUS, checkBox.isChecked());
parseObject.put(ObjConst.POST_AUTHOR, parseUser);
parseObject.put(ObjConst.POST_TEXT, txt);
final Context ctx = this;
if (pFile != null) {
parseObject.put(ObjConst.POST_PHOTO, pFile);
Log.d("MY_TAG", "Start upload.");
pFile.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e != null) {
Log.d("MY_TAG", "ParseException: " + e.getMessage());
}
Log.d("MY_TAG", "Start save ParseObject");
saveParseObject(parseObject, ctx);
}
}, new ProgressCallback() {
@Override
public void done(Integer percentDone) {
Log.d("MY_TAG", percentDone+" %");
}
});
} else saveParseObject(parseObject, ctx);
}
}
void saveParseObject(ParseObject parseObject, final Context ctx) {
Log.d("MY_TAG", "Start saveParseObject()");
parseObject.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
Log.d("MY_TAG", e.getMessage());
Log.d("MY_TAG", "End. ParseObject saved. " + e.getMessage());
if (e != null)
Log.i("Write post.", "Can`t write post. Problem with internet connection.");
else Toast.makeText(ctx,
"Отправлено", Toast.LENGTH_SHORT).show();
}
});
}
计划的输出:
01-04 21:46:02.376 7360-7360/com.appserv D/MY_TAG: Start upload.
01-04 21:46:02.433 7360-7360/com.appserv D/MY_TAG: 58 %
01-04 21:46:02.434 7360-7360/com.appserv D/MY_TAG: 100 %
01-04 21:46:13.615 7360-7360/com.appserv D/MY_TAG: 58 %
01-04 21:46:13.618 7360-7360/com.appserv D/MY_TAG: 100 %
01-04 21:46:25.941 7360-7360/com.appserv D/MY_TAG: 58 %
01-04 21:46:25.945 7360-7360/com.appserv D/MY_TAG: 100 %
01-04 21:46:40.553 7360-7360/com.appserv D/MY_TAG: 58 %
01-04 21:46:40.557 7360-7360/com.appserv D/MY_TAG: 100 %
01-04 21:46:59.725 7360-7360/com.appserv D/MY_TAG: 58 %
01-04 21:46:59.730 7360-7360/com.appserv D/MY_TAG: 100 %
01-04 21:47:09.754 7360-7360/com.appserv D/MY_TAG: ParseException: i/o failure
01-04 21:47:09.754 7360-7360/com.appserv D/MY_TAG: Start save ParseObject
01-04 21:47:09.754 7360-7360/com.appserv D/MY_TAG: Start saveParseObject()
这是什么?)
答案 0 :(得分:0)
尝试以这种方式成功上传到解析数据库中的任何类型的图像。
String first = "your image Path";
final ParseFile Parseimagecam1;
int targetW = 450;
int targetH = 800;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(first, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
// Decode the image file into a Bitmap sized to fill the
// View
// imgHome.setImageBitmap(bitmap);
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor << 1;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(first, bmOptions);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] img1 = stream.toByteArray();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
Parseimagecam1 = new ParseFile("IMG_" + timeStamp + ".jpg", img1);
Parseimagecam1.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
// TODO Auto-generated method stub
if (e == null) {
// success
// here call your new method for upload image like
uploadImage(Parseimagecam1);
} else {
// error
}
}
});
}
protected void uploadImage(ParseFile parseimagecam1) {
// TODO Auto-generated method stub
final ParseObject parseObject = new ParseObject(ObjConst.POST);
parseObject.put("keyName", parseimagecam1);
parseObject.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
// TODO Auto-generated method stub
if (e == null) {
// success check your data base
} else {
// fail
}
}
});
}
它为我完成了工作。 我已经在解析表中上传了最多6张图片。