编辑:我几乎用您的评论发布了答案。您可以使用类似的方法将您的图像从assets 转换为Bitmap。
InputStream bitmap = null;
try {
bitmap = getAssets().open("icon.png");
bmpImageGallery = BitmapFactory.decodeStream(bitmap);
} catch (IOException e) {
e.printStackTrace();
} finally {
bitmap.close();
}
这就是我通过 onActivityResult 方法中的 Intent 显示图库中的图像的方式:
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
}
}