我想在将图片上传到服务器之前减少照片大小。如何做到这一点没有任何影响像图像优化?
我也希望允许用户裁剪图片,因此我找到了一些教程,但通过的意见,我才知道,用户在某些版本的Android得错误。所以有没有什么办法让用户裁剪图像,可以在所有Android版本中使用。在将图片上传到服务器之前,在Android中缩小图片的大小
2
A
回答
1
试一下这个它的工作对我来说:
private Bitmap getBitmap(String path) {
Uri uri = getImageUri(path);
InputStream in = null;
try {
final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
in = mContentResolver.openInputStream(uri);
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
while ((o.outWidth * o.outHeight) * (1/Math.pow(scale, 2)) >
IMAGE_MAX_SIZE) {
scale++;
}
Log.d(TAG, "scale = " + scale + ", orig-width: " + o.outWidth + ",
orig-height: " + o.outHeight);
Bitmap b = null;
in = mContentResolver.openInputStream(uri);
if (scale > 1) {
scale--;
// scale to max possible inSampleSize that still yields an image
// larger than target
o = new BitmapFactory.Options();
o.inSampleSize = scale;
b = BitmapFactory.decodeStream(in, null, o);
// resize to desired dimensions
int height = b.getHeight();
int width = b.getWidth();
Log.d(TAG, "1th scale operation dimenions - width: " + width + ",
height: " + height);
double y = Math.sqrt(IMAGE_MAX_SIZE
/(((double) width)/height));
double x = (y/height) * width;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x,
(int) y, true);
b.recycle();
b = scaledBitmap;
System.gc();
} else {
b = BitmapFactory.decodeStream(in);
}
in.close();
Log.d(TAG, "bitmap size - width: " +b.getWidth() + ", height: " +
b.getHeight());
return b;
} catch (IOException e) {
Log.e(TAG, e.getMessage(),e);
return null;
}
2
使用下面的代码作物和减少图像的大小,而从库设备的
Intent photoPickerIntent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra("crop", "true");
photoPickerIntent.putExtra("outputX", 512);
photoPickerIntent.putExtra("outputY", 512);
photoPickerIntent.putExtra("aspectX", 1);
photoPickerIntent.putExtra("aspectY", 1);
photoPickerIntent.putExtra("scale", true);
File f = new File(android.os.Environment
.getExternalStorageDirectory(), "tt.jpg");
希望它捡起来helps..Happy编码:)
2
public Bitmap decodeAndResizeFile(File f) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp/2 < REQUIRED_SIZE
|| height_tmp/2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
convert your bitmap object to file on=bject and use these ::->
File file = new File("your bitmap path ");
Bitmap bmpp = decodeAndResizeFile(file);
相关问题
- 1. 在将图片上传到Firebase之前调整图片大小
- 2. 将图片大小缩小到WordPress的缩略图大小
- 3. 图片上传到服务器在android
- 4. [Android] - 将图片上传到服务器
- 5. 上传前调整图片的大小
- 6. 在发送到服务器之前,android会将图像大小压缩
- 7. 将图片压缩成Android上传到服务器的过程
- 8. 在上传HTML文件之前调整图片的大小5
- 9. 上传前检查图片大小
- 10. 上传图片到服务器的android
- 11. Facebook图片照片缩略图大小
- 12. 缩放图片框大小图片
- 13. PHP等待图片上传到完成之前调整大小
- 14. 图片大小Android上传PHP
- 15. swift ios在上传之前缩小图像大小
- 16. 上传图片前获取图片大小php
- 17. Android图片大小
- 18. Xamarin:将图片上传到服务器
- 19. 从图库中上传图片到服务器在android
- 20. Google App Engine Python:获取图片上传大小服务器端
- 21. Android服务:将图片上传到http服务器
- 22. 敏捷上传器(在上传到服务器之前调整图片大小) - POST数据中根本没有图片?
- 23. 点击图片将图片缩小后缩小
- 24. 将图片上传到服务器时重命名图片
- 25. Android的图片上传AWS服务器
- 26. Android图片资源缩小
- 27. 如何将Android图库中的照片上传到服务器?
- 28. 上传图片在服务器上iPhone
- 29. 如何在上传到解析为PFFile之前缩小图像的大小? (Swift)
- 30. 上传时缩小图片尺寸
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html –