【发布时间】:2014-11-26 08:54:28
【问题描述】:
我正在开发一个应用程序,其中一部分是从手机图库中选择图像并将其作为字符串发布到 web api。问题在于我选择的图像并将其转换为要发送的字符串。这是我的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_picker);
((Button) findViewById(R.id.buttonSelectMedia))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
photoPickerIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(photoPickerIntent, 2);
}
});
}
还有我的 onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] arr = baos.toByteArray();
String stringImage = Base64.encodeToString(arr, Base64.DEFAULT);
String fileName = imageReturnedIntent.getData().getLastPathSegment();
}
我的内存不足
String stringImage = Base64.encodeToString(arr, Base64.DEFAULT);
大家好。
【问题讨论】:
-
真正的问题是什么?图片有多大?你的设备有多少内存?问题似乎很明显。您的应用占用了太多内存。