Android 上传前压缩位图
我使用Facebook Parse将位图上传到服务器(因此在本例中,我希望使用PNG/JPEG格式,因为WEBP会给出一个错误)。我能够成功上传图像,我想知道的是,它是否可以更有效,如果我可以进一步降低JPEG的质量?(注意:图像将全屏显示)Android 上传前压缩位图,android,bitmap,android-bitmap,Android,Bitmap,Android Bitmap,我使用Facebook Parse将位图上传到服务器(因此在本例中,我希望使用PNG/JPEG格式,因为WEBP会给出一个错误)。我能够成功上传图像,我想知道的是,它是否可以更有效,如果我可以进一步降低JPEG的质量?(注意:图像将全屏显示) 你的意思是除了从这里显示的70降低压缩质量之外?请参考@DougStevenson Yes。还有,如果我能降到70以下?在不影响所有手机屏幕的图像质量的情况下,我可以降低到多低?这听起来像是让你自己试验一下,看看什么是可以接受的。 public v
你的意思是除了从这里显示的70降低压缩质量之外?请参考@DougStevenson Yes。还有,如果我能降到70以下?在不影响所有手机屏幕的图像质量的情况下,我可以降低到多低?这听起来像是让你自己试验一下,看看什么是可以接受的。
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;
}