所以我正在使用上传字符串路径到imageViews,它工作正常,但我的IOS应用程序不能使用我的字符串路径,所以我需要切换到基本64字符串。如何将我的字符串路径转换为基本64位图像并将其上传到imageview而不会出现内存不足错误?
以下是我目前在myActivityResult()上的内容。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
addImageImageView.setVisibility(View.GONE);
if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK && data != null) {
//First we gotta make sure to add the images to
ArrayList<Image> imagesFromGallery = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);//Image is a personal object I made.
for (int i = 0; i < imagesFromGallery.size(); i++) {
images.add(imagesFromGallery.get(i).path);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(20, 20, 0, 0);//5dp = 20 pixels
lp.height = 720;//180dp = 720pixels
lp.width = 1400;//330dp = 1320 pixels.
ImageView newImageView = new ImageView(this);
newImageView.setLayoutParams(lp);
Glide.with(this).load(imagesFromGallery.get(i)).centerCrop().into(newImageView);
imageLinearLayout.addView(newImageView, 0);
}
}
答案 0 :(得分:0)
您可以使用Base64 Android类:
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
您必须先将图像转换为字节数组。这是一个例子:
Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
所以byteArrayImage是b。
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);