2015-05-12 26 views
3

我正試圖在Android應用程序中將從移動設備捕獲的圖像文件保存爲解析。我正在使用final File mediaFile。我認爲我的問題是,我知道我需要將文件轉換爲一個字節數組...我嘗試了各種方法來做到這一點。我可以在預覽中看到圖像...但我似乎無法將其保存到Parse。圖像捕捉,保存文件... Android /解析

感謝您的任何幫助。

public Uri getOutputMediaFileUri(int type) { 
     return Uri.fromFile(getOutputMediaFile(type)); 

    } 


    /** 
    * 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 
       byte[] data2; 

       FileInputStream fis = new FileInputStream(mediaFile); 
       FileChannel fc = fis.getChannel(); 
       int sz = (int)fc.size(); 
       MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); 
       data2 = new byte[bb.remaining()]; 
       bb.get(data2); 
       // Save the image to Parse 
       photoFile = new ParseFile("profile_photo.jpg", data2); 
       photoFile.saveInBackground(); 
       mCurrentUser.getCurrentUser(); 
       mCurrentUser.put("ProfilePhoto", photoFile); 
       mCurrentUser.saveInBackground(); 

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

      } 

      return mediaFile; 
     } 


    } 

回答

2

我已經試過這個,它的工作對我來說......!

currentUser = ParseUser.getCurrentUser(); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
// Compress image to lower quality scale 1 - 100 
//here bitmap is your selected photo's bitmap  
bitmapProfilePic.compress(Bitmap.CompressFormat.PNG, 100, stream); 
byte[] image = stream.toByteArray(); 
final ParseFile file = new ParseFile("Profile.png", image); 
file.saveInBackground(new SaveCallback() { 
     @Override 
     public void done(ParseException e) { 
      if (e == null) { 

      currentUser.put("ProfilePhoto", file); 
      currentUser.saveInBackground(new SaveCallback() { 
        @Override 
        public void done(ParseException e) { 
          if (e == null) { 
            Toast.makeText(context, "Updated..!!", Toast.LENGTH_LONG).show(); 
           } 
          } 
         }); 
         Toast.makeText(context, "Picture Updated..!!", Toast.LENGTH_LONG).show(); 
        } else { 
         Toast.makeText(context, "Error to " + e.getMessage(), Toast.LENGTH_LONG).show(); 
        } 
       } 
      }); 
你的情況

您已經字節數據2這樣的數據,...

final ParseFile file = new ParseFile("YourPhotoName.jpg", data2); 
file.saveInBackground(new SaveCallback() { 
     @Override 
     public void done(ParseException e) { 
      if (e == null) { 

      currentUser.put("ProfilePhoto", file); 
      currentUser.saveInBackground(new SaveCallback() { 
        @Override 
        public void done(ParseException e) { 
          if (e == null) { 
            Toast.makeText(context, "Updated..!!", Toast.LENGTH_LONG).show(); 
           } 
          } 
         }); 
         Toast.makeText(context, "Picture Updated..!!", Toast.LENGTH_LONG).show(); 
        } else { 
         Toast.makeText(context, "Error to " + e.getMessage(), Toast.LENGTH_LONG).show(); 
        } 
       } 
      }); 
+0

謝謝...所以,該媒體文件變量去哪裏bitmapProfilePic是在你的代碼?在我的代碼中,它是在這裏創建的:mediaFile = new File(mediaStorageDir.getPath()+ File.separator +「IMG_」+ timeStamp +「.jpg」); –

+0

我的代碼中沒有bitmapProfilePic是所選圖像的位圖。所以你必須把它轉換成位圖才能使用這個代碼。但是你有byte []數據,所以你可以直接使用它。 – Nils

+0

啊,所以我有byte [] bFile = new byte [(int)mediaFile.length()]; ..所以我會把:bFile.compress(Bitmap.CompressFormat.PNG,100,stream);? –

0

您可以嘗試降低大小的圖像比PNG照相壓縮成JPEG格式,JPEG壓縮結果圖片。

試試這個代碼:在我的代碼

bitmapProfilePic.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
相關問題