2017-01-31 5 views
0

私はそのようなコードを手に入れました。実際にうまくいきました。私が持っている唯一の懸念は、画像をbase64に変換することで、たくさんのメモリを食べているようです。Androidで画像同期にメモリを保存する

もっと良い方法があるかどうかを知りたいです。それはメモリに文字列全体をロードし、大きな画像をOOM例外を引き起こす可能性があるので

// 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); 
    } 

よろしく、

答えて

0

それは大量のメモリを食べる理由があります。

あなたはメモリを節約し、

+0

OOM例外を防ぐために、データのチャンクに画像をアップロードするためのより良いイメージをアップロードし、あなたがより詳細にしてください説明できますか?イメージを大量のデータでアップロードするにはどうすればよいですか?ありがとう。 –

+0

あなたはそれを行う方法を検索するGoogleを使用することができます – tyczj

関連する問題