2015-05-29 95 views
2

我可以使用以下代碼上傳jpeg圖像和png圖像。但是,當我嘗試上傳具有相同代碼的gif圖像時,它會保存爲靜態文件並且不顯示動畫。我正在使用glide來顯示gif,並且它顯示了一個動畫gif url。這裏是用於上傳圖像的代碼如何上傳gif文件在android中解析爲解析文件

public void createPost(String content, boolean imageAttached, Bitmap bitmap) { 
    if (DuzooActivity.isNetworkAvailable()) { 
     if (dialog != null) { 
      dialog.show(); 
      dialog.setCancelable(false); 
     } 
     final ParseObject post = new ParseObject("Post"); 
     post.put(DuzooConstants.PARSE_POST_CONTENT, content); 
     post.put(DuzooConstants.PARSE_POST_USER_NAME, 
       DuzooPreferenceManager.getKey(DuzooConstants.KEY_USER_NAME)); 
     post.put(DuzooConstants.PARSE_POST_USER_IMAGE, 
       DuzooPreferenceManager.getKey(DuzooConstants.KEY_USER_IMAGE)); 
     post.put(DuzooConstants.PARSE_POST_UPVOTES, 0); 
     post.put(DuzooConstants.PARSE_POST_DOWNVOTES, 0); 
     post.put(DuzooConstants.PARSE_POST_IS_FLAGGED, false); 
     post.put(DuzooConstants.PARSE_POST_FACEBOOK_ID, 
       DuzooPreferenceManager.getKey(DuzooConstants.KEY_FACEBOOK_ID)); 
     post.put(DuzooConstants.PARSE_POST_INTEREST_TYPE, 
       DuzooPreferenceManager.getIntKey(DuzooConstants.KEY_INTEREST_TYPE)); 
     post.put(DuzooConstants.PARSE_POST_TIMESTAMP, System.currentTimeMillis()); 
     post.put(DuzooConstants.PARSE_POST_COMMENT_COUNT, 0); 
     post.put(DuzooConstants.PARSE_POST_HAS_MEDIA, imageAttached); 
     post.put(DuzooConstants.PARSE_POST_FAVORITE, false); 
     post.put(DuzooConstants.PARSE_POST_MY_VOTE, 0); 
     post.put(DuzooConstants.PARSE_POST_DELETED,false); 
     if (imageAttached) { 
      String name = path.substring(path.lastIndexOf("/")); 


      image = new ParseFile(name, Util.convertBitmapToBytes(bitmap)); 
      image.saveInBackground(new SaveCallback() { 
       @Override 
       public void done(ParseException e) { 
        if (e == null) { 
         post.put(DuzooConstants.PARSE_POST_IMAGE, image); 
         post.pinInBackground(new SaveCallback() { 
          @Override 
          public void done(ParseException e) { 
           if (dialog.isShowing()) 
            dialog.dismiss(); 
          } 
         }); 
         post.saveInBackground(); 
        } 
       } 
      }); 
     } else { 
      post.pinInBackground(new SaveCallback() { 
       @Override 
       public void done(ParseException e) { 
        if (dialog.isShowing()) 
         dialog.dismiss(); 
        returnToHomeActivity(); 
       } 
      }); 
      post.saveInBackground(new SaveCallback() { 
       @Override 
       public void done(ParseException e) { 
       } 
      }); 
     } 
    } else 
     Toast.makeText(this, "Sorry, no internet connection available", Toast.LENGTH_SHORT).show(); 
} 

這裏是convertToBytes函數。

public static byte[] convertBitmapToBytes(Bitmap bitmap) { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 40, baos); 
    byte[] b = baos.toByteArray(); 
    return b; 
} 

有沒有一種方法可以在上面的代碼中調整上傳gif文件或者是否有更好的方法。由於

回答

2

我得到的答案上述problem`

File file = new File(path); 
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
       try { 
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); 

        int read; 
        byte[] buff = new byte[1024]; 
        while ((read = in.read(buff)) > 0) { 
         out.write(buff, 0, read); 
        } 
        out.flush(); 
        byte[] bytes = out.toByteArray(); 

        image = new ParseFile(name, bytes); 
       } catch (FileNotFoundException ex) { 
       } catch (IOException ex) { 
       }