2017-09-06 110 views
1

好吧,我不認爲這是cn1問題,但我用盡了選項。我有一個應用程序,並且我一直在使用Capture.capturePhoto爲用戶提供用他們的設備拍攝照片並將其上傳到我的服務器的功能。現在已經工作了好幾個月了。我被要求添加從手機照片庫上傳照片的能力,我認爲這將是微不足道的,但在弄清楚如何縮放圖像並將其存儲在存儲器中之後,我現在只是發送0字節到我的服務器。從存儲不上傳Temp png文件

我知道這不是我的服務器,它並沒有改變,仍然可以上傳各種來源的文件,包括cn1應用程序中的照片。

下面是上傳照片

private void uploadImage(String filename, String name) throws IOException { 
    String ext = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase(); 
    if (ext.equals("jpg") || ext.equals("jpeg") || ext.equals("pjpeg") || ext.equals("png")) { 
     MultipartRequest request = new MultipartRequest() { 
      Hashtable h; 
      @Override 
      protected void readResponse(InputStream input) throws IOException { 
       JSONParser p = new JSONParser(); 
       h = p.parse(new InputStreamReader(input)); 
       super.readResponse(input); 
      } 

      @Override 
      protected void postResponse() { 
       List<Map<String, Object>> media = (List<Map<String, Object>>)h.get("media"); 
       Dialog.show("Photo Uploaded", media.size() + " photo(s) uploaded and available to send.", "OK", null); 
      } 
     }; 
     request.setUrl(MyApplication.IMGSERVER); 
     request.setPost(true); 
     request.addData(name, filename, "image/" + ext); 
     request.addRequestHeader("X-Dart-Token", token); 
     NetworkManager.getInstance().addToQueue(request); 
    } else { 
     Dialog.show("Invalid Photo Format", "The photo format (" + ext+ ") is not supported. Try with jpg or png.", "OK", null); 
    } 
} 

這裏是獲取和規模從畫廊的照片的代碼的相關部分的代碼

Display.getInstance().openGallery(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent actionEvent) { 
     if (actionEvent != null && actionEvent.getSource() != null) { 
     String filepath = (String)actionEvent.getSource(); 
     if (filepath != null) { 
      // scale down the image 
      String filename = "tempfile.png"; 
      boolean success = false; 
      if (Storage.isInitialized()) { 
       try { 
        OutputStream out = Storage.getInstance().createOutputStream(filename); 
        ImageIO.getImageIO().save(filepath, out, ImageIO.FORMAT_PNG, 500, -1, 1); 
        out.close(); 
        success = true; 
       } catch (IOException e1) { 
        Log.p("image scaling failed " + e1.getMessage()); 
        Dialog.show("Photo Upload", "Unable to scale photo:" + e1.getMessage(), "OK", null); 
       } 
       if (success) { 
        // open dialog and have user provide name 
        try { 
         String tmpFilepath = FileSystemStorage.getInstance().getAppHomePath() + filename; 
         Dialog.show("Image File", tmpFilepath, "OK", null); 
         uploadImage(tmpFilepath, name); 
        } catch (IOException err) { 
         Log.p("Image Upload Failed:" + err.getMessage()); 
         Dialog.show("Picture Uploaded Failed", "Something prevented the upload of the image.", "OK", null); 
        } 
       } 
      } 
     } 
    } 
} 

我特意剪了很多的周圍的代碼在上面的片段集中於我認爲重要的事情。

我留在了用於調試的對話框語句中,以便我可以看到設備上的路徑。如果我在模擬器中運行它,並選擇一個文件,它可以很好地上傳,但是當我在iPhone上運行它時,發送到服務器的文件有0個字節,並且uploadImage方法中的對話框將顯示'0照片( s)上傳'。當在「tempfile.png」上使用Storage.g.entrySize()時,它顯示文件的大小不是0。我怎樣才能正確地引用這個文件來將它發送到服務器。

回答

1

您正在將圖庫圖像保存爲Storage,但上傳方法與FileSystemStorage協同工作,那些圖像不兼容,因此您會收到不兼容的行爲。

+0

我改變了這一行 OutputStream out = Storage.getInstance()。createOutputStream(filename); 至 OutputStream out = FileSystemStorage.getInstance()。openOutputStream(filename); 並得到相同的結果。什麼是正確的方法? –

+0

在'FileSystemStorage'中,你需要一個完整的絕對路徑,而不是文件名。所以你需要預先安裝應用程序的主目錄路徑。 –

+0

我認爲這就是我所做的,但我仍然得到0字節。 String tmpFilepath = FileSystemStorage.getInstance()。getAppHomePath()+ filename; [Here](https://s3-us-west-2.amazonaws.com/tikiwade/IMG_2887.PNG)是一個顯示tmpFilepath值的對話框。 –