2015-05-06 51 views
0

我正在嘗試保存照片解析。照片正在拍攝,我可以預覽它,但是,我沒有看到Parse後端上的文件。文件不保存爲ParseFile,Android

這裏是我的代碼:

* returning image/video 
     */ 
     private File getOutputMediaFile(int type) { 

      // External sdcard location 
      String appName = CameraActivity.this.getString(R.string.app_name); 
      File mediaStorageDir = new File(
        Environment 
          .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
        appName); 

      // Create the storage directory if it does not exist 
      if (!mediaStorageDir.exists()) { 
       if (!mediaStorageDir.mkdirs()) { 
        Log.d(appName, "Oops! Failed create " 
          + appName + " directory"); 
        return null; 
       } 
      } 

      // Create a media file name 
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
        Locale.getDefault()).format(new Date()); 
      final File mediaFile; 
      if (type == MEDIA_TYPE_IMAGE) { 
       mediaFile = new File(mediaStorageDir.getPath() + File.separator 
         + "IMG_" + timeStamp + ".jpg"); 
      } else { 
       return null; 
      } 




      final ParseFile photoFile; 

      FileInputStream fileInputStream=null; 

      byte[] bFile = new byte[(int) mediaFile.length()]; 

      try { 
       //convert file into array of bytes 
       fileInputStream = new FileInputStream(mediaFile); 
       fileInputStream.read(bFile); 
       fileInputStream.close(); 

       for (int i = 0; i < bFile.length; i++) { 
        System.out.print((char)bFile[i]); 
       } 

       System.out.println("Done"); 
      }catch(Exception e) { 
       e.printStackTrace(); 

      } 


      // Save the image to Parse 
      photoFile = new ParseFile("profile_photo.jpg", bFile); 
      photoFile.saveInBackground(); 

      mCurrentUser.getCurrentUser(); 
      mCurrentUser.put("ProfilePhoto", photoFile); 
      mCurrentUser.saveInBackground(); 

      return mediaFile; 
     } 

我用這作爲參考:https://www.parse.com/docs/android/guide#files我也意識到我需要在字節格式的數據。所以我試圖做到這一點,但可能沒有做到這一點。下面

回答

1

樣本使用Apache的HttpClient,而不是parse.android.SDK.saveInBackground()...但它可以幫助你映射文件的字節

   FileInputStream fis = new FileInputStream(mfile); 
       FileChannel fc = fis.getChannel(); // Get the file's size and then map it into memory 
       int sz = (int)fc.size(); 
       MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); 
       data2 = new byte[bb.remaining()]; 
       bb.get(data2); 
       ByteArrayEntityHC4 reqEntity = new ByteArrayEntityHC4(data2); 
       httpPost.setEntity(reqEntity); 
       fis.close();