节省安卓图像同步内存



我得到了一段这样的代码,它实际上工作正常。我唯一担心的是它将图像转换为base64,并且似乎消耗了大量内存。

我很想知道是否有更好的方法可以做到这一点。

// SYNCING IMAGES
    private boolean syncImages() {
        boolean update = false;
        for (Asset asset : assets) {
            // Get all images for the asset (list) -> get string name of image (string)
            List<Image> remoteAssetImages = RESTfulService.getInstance().getAssetImages(asset.getId());
            ArrayList<String> remoteAssetURIs = new ArrayList<>();
            for (Image img : remoteAssetImages)
                remoteAssetURIs.add(img.getImageId() + ".jpg");
            for (String remoteURI : remoteAssetURIs) {
                if (!asset.getImages().contains(remoteURI)) {
                    long imageId = Long.parseLong(remoteURI.replace(".jpg", ""));
                    // Perform synchronous get request
                    try {
                        Bitmap img = Picasso.with(getApplicationContext())
                                .load("http://www.google.com/image" + imageId + "/jpg")
                                .get();
                        saveRemoteImage(getApplicationContext(), img, imageId, asset);
                        asset.setConcatenatedImageURI();
                        AssetDatabaseHandler.getInstance(getApplicationContext()).updateAsset(asset);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    update = true;
                }
            }
            Iterator<String> uriIterator = asset.getImages().iterator();
            while(uriIterator.hasNext()){
                String uri = uriIterator.next();
                if (!remoteAssetURIs.contains(uri)) {
                    String base64Image = getBase64EncodedImage(getApplicationContext(), uri);
                    ImageData postImage = new ImageData(6, asset.getId(), base64Image);
                    RESTfulService.getInstance().postImage(postImage);
                    renameImageFile(getApplicationContext(), asset, uri, postImage.getImageId()+".jpg");
                    AssetDatabaseHandler.getInstance(getApplicationContext()).updateAsset(asset);
                }
            }
        }
        return update;
    }
    public static void renameImageFile(Context context, Asset asset, String currentName, String newName){
        File originalFile = context.getFileStreamPath(currentName);
        File newFile = new File(originalFile.getParent(), newName);
        if (newFile.exists())
            context.deleteFile(newName);
        originalFile.renameTo(newFile);
        asset.getImages().remove(currentName);
        asset.addImage(newName);
        asset.setConcatenatedImageURI();
    }
    public static String getBase64EncodedImage(Context context, String imagePath){
        Bitmap bmp = getImageBitmap(context, imagePath);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] imageDataBytes = stream.toByteArray();
        return Base64.encodeToString(imageDataBytes, Base64.NO_WRAP);
    }

问候

它占用大量内存的原因是因为它将整个字符串加载到内存中,并可能导致具有大图像的 OOM 异常。

上传图像时,最好以数据块的形式上传图像,以节省内存并防止 OOM 异常

最新更新