我使用Facebook-Parse将位图上传到服务器(因此在这种情况下我想使用PNG / JPEG格式,因为WEBP会出错)。我能够成功上传图像,我想知道的是它是否可以提高效率,是否可以进一步降低JPEG的质量? (注意:图像将全屏查看)。
public void save_DetailStory_ToParse()
{
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
String filePath = iDataSave_path;
final Bitmap bitmap = decodeSampledBitmapFromFilePath(getResources(), width, height, filePath.toString());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
byte[] image = stream.toByteArray();
ParseFile file = new ParseFile("androidbegin.jpeg", image);
pObject_storyDetail.put(KEY_IMAGE_FILE, file);
pObject_storyDetailList.add(pObject_storyDetail);
}
public static Bitmap decodeSampledBitmapFromFilePath(Resources res, int reqWidth, int reqHeight, String filePath)
{
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
//int inSampleSize = 1;
int inSampleSize = 2;
if (height > reqHeight || width > reqWidth)
{
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth)
{
inSampleSize *= 2;
}
}
return inSampleSize;
}