Android 从 sd 卡中选取图像,调整图像大小并将其保存回 sd 卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11688982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pick image from sd card, resize the image and save it back to sd card
提问by Ashish Kumar
I am working on an application, in which I need to pick an image from sd card
and show it in image view. Now I want the user to decrease/increase its width by clicking a button and then save it back to the sd card.
我正在开发一个应用程序,我需要从中选择一个图像sd card
并在图像视图中显示它。现在我希望用户通过单击按钮来减小/增加其宽度,然后将其保存回 SD 卡。
I have done the image picking and showing it on ui. But unable to find how to resize it.Can anyone please suggest me how to achieve it.
我已经完成了图像选择并将其显示在 ui 上。但无法找到如何调整它的大小。任何人都可以建议我如何实现它。
回答by MAC
Just yesterday i have done this
就在昨天,我做了这个
File dir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
Bitmap b= BitmapFactory.decodeFile(PATH_ORIGINAL_IMAGE);
Bitmap out = Bitmap.createScaledBitmap(b, 320, 480, false);
File file = new File(dir, "resize.png");
FileOutputStream fOut;
try {
fOut = new FileOutputStream(file);
out.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
b.recycle();
out.recycle();
} catch (Exception e) {}
Also don't forget to recycle your bitmaps
: It will save memory.
也不要忘记回收你的bitmaps
:它会节省内存。
You can also get path of new created file String: newPath=file.getAbsolutePath();
您还可以获取新创建的文件字符串的路径: newPath=file.getAbsolutePath();
回答by Dima Rostopira
Solution without OutOfMemoryException
in Kotlin
没有OutOfMemoryException
Kotlin 的解决方案
fun resizeImage(file: File, scaleTo: Int = 1024) {
val bmOptions = BitmapFactory.Options()
bmOptions.inJustDecodeBounds = true
BitmapFactory.decodeFile(file.absolutePath, bmOptions)
val photoW = bmOptions.outWidth
val photoH = bmOptions.outHeight
// Determine how much to scale down the image
val scaleFactor = Math.min(photoW / scaleTo, photoH / scaleTo)
bmOptions.inJustDecodeBounds = false
bmOptions.inSampleSize = scaleFactor
val resized = BitmapFactory.decodeFile(file.absolutePath, bmOptions) ?: return
file.outputStream().use {
resized.compress(Bitmap.CompressFormat.JPEG, 75, it)
resized.recycle()
}
}
回答by Nermeen
Try using this method:
尝试使用此方法:
public static Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight) {
if(bitmapToScale == null)
return null;
//get the original width and height
int width = bitmapToScale.getWidth();
int height = bitmapToScale.getHeight();
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(newWidth / width, newHeight / height);
// recreate the new Bitmap and set it back
return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, true);
}
回答by VinceFR
回答by Robert Pal
I found this useful library for this achievement: https://github.com/hkk595/Resizer
我为这个成就找到了这个有用的库:https: //github.com/hkk595/Resizer
回答by Dhruv Gairola
You should ideally use multitouch instead of using a button to increase/decrease width. Here'san amazing library. Once the user decides to save the image, the image translation matrix must be stored persistently (in your sqlite database). Next time the user opens the image, you need to recall the matrix and apply it to your image.
理想情况下,您应该使用多点触控而不是使用按钮来增加/减少宽度。这是一个了不起的图书馆。一旦用户决定保存图像,图像转换矩阵必须永久存储(在您的 sqlite 数据库中)。下次用户打开图像时,您需要调用矩阵并将其应用于您的图像。
I've actually done this before.
我以前确实这样做过。