编辑:我几乎用你的评论发布了答案。您可以使用类似的东西将您的 Image 从assets
转换为Bitmap
.
InputStream bitmap = null;
try {
bitmap = getAssets().open("icon.png");
bmpImageGallery = BitmapFactory.decodeStream(bitmap);
} catch (IOException e) {
e.printStackTrace();
} finally {
bitmap.close();
}
这就是我通过方法中的 Intent 从图库中显示图像的onActivityResult
方式:
targetURI = data.getData();
try {
bmpImageGallery = MediaStore.Images.Media.getBitmap(this.getContentResolver(), targetURI);
// SET THE IMAGE FROM THE GALLERY TO THE IMAGEVIEW
imgvwSelectedImage.setImageBitmap(bmpImageGallery);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
这是上传图片的代码:
byte[] data = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmpImageGallery.compress(CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
Bundle postImgGallery = new Bundle();
// ADD THE PHOTO DATA TO THE BUNDLE
postImgGallery.putByteArray("photo", data);
// ADD THE CAPTION FROM THE STRING finalStatusMessage TO THE BUNDLE
if (finalStatusMessage.equals("")) {
/***** DO NOTHING HERE *****/
} else {
postImgGallery.putString("caption", finalStatusMessage);
}
Utility.mAsyncRunner.request(userID + "/photos", postImgGallery, "POST", new PhotoUploadListener(), null);
注意:在此位"caption", finalStatusMessage
中,caption
也可以替换为message
。我从来没有看到使用其中任何一个的帖子有任何区别。但为了安全起见,请在使用前进行检查。;-)
该类用于检查上传的状态:
private class PhotoUploadListener extends BaseRequestListener {
@Override
public void onComplete(String response, Object state) {
// DISPLAY A CONFIRMATION TOAST
}
}