我正試圖在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;
}
}
謝謝...所以,該媒體文件變量去哪裏bitmapProfilePic是在你的代碼?在我的代碼中,它是在這裏創建的:mediaFile = new File(mediaStorageDir.getPath()+ File.separator +「IMG_」+ timeStamp +「.jpg」); –
我的代碼中沒有bitmapProfilePic是所選圖像的位圖。所以你必須把它轉換成位圖才能使用這個代碼。但是你有byte []數據,所以你可以直接使用它。 – Nils
啊,所以我有byte [] bFile = new byte [(int)mediaFile.length()]; ..所以我會把:bFile.compress(Bitmap.CompressFormat.PNG,100,stream);? –