Java – Base64 coding takes too long in Android
•
Java
I capture images with my camera I save the file in the public photo directory and save URI to the file
I want to save the image in the base64 string, put it on the HashMap, and then put it into the XML file
protected Void doInBackground(Void...voids) { options.inJustDecodeBounds = false; //Bitmap bmp = BitmapFactory.decodeFile(imageFilePath,options); InputStream in = null; try { in = getContentResolver().openInputStream(Uri.parse(mCurrentPhotoPath)); } catch (FileNotFoundException e) { e.printStackTrace(); } options.inSampleSize = 2; Bitmap image = BitmapFactory.decodeStream(in,null,options); int imgHeight = image.getHeight(); int imgWidth = image.getWidth(); while(imgHeight>2000){ imgHeight = imgHeight / 2; } while(imgWidth>2000){ imgWidth = imgWidth / 2; } Bitmap test = Bitmap.createScaledBitmap(image,imgWidth,imgHeight,false); String stest = base64EncodeDecode.encodeToBase64(test); items.put("image",base64EncodeDecode.encodeToBase64(test); return null; }
Base64 takes a long time to encode it Encodetobase64 method
public String encodeToBase64(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG,100,baos); byte[] b = baos.toByteArray(); return Base64.encodeToString(b,Base64.DEFAULT); }
Can you tell me if I made a mistake in coding?
I hope my question is clear
Cordial greetings!
Solution
If you get!!! Failed adhesive transaction! The error may be because you have passed a lot of data to other activities. How many restrictions can you send Try compressing the image to 50% or 30% image compress(Bitmap.CompressFormat.JPEG,50,baos);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码