【发布时间】:2014-07-17 07:43:49
【问题描述】:
您好,我有 facebook 图像,我必须压缩并将其放在我的 imageview 中。我使用下面的代码来调整我的图像大小并压缩它,以便我可以在我的图像视图中显示它,但它给出了文件未找到异常错误
【问题讨论】:
标签: android facebook compression image-resizing
您好,我有 facebook 图像,我必须压缩并将其放在我的 imageview 中。我使用下面的代码来调整我的图像大小并压缩它,以便我可以在我的图像视图中显示它,但它给出了文件未找到异常错误
【问题讨论】:
标签: android facebook compression image-resizing
我找不到任何方法来压缩位于服务器上的文件/图像。 你可以从 URL 中获取位图,然后你想重新调整大小。
用于从 URL 获取位图。
URL url = new URL("http://....");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
你可以使用下面的代码来调整大小
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
如何使用:- 放置此代码:-
private void showImage(final String URL) {
new Thread(new Runnable() {
@Override
public void run() {
URL url = new URL(URL);
Bitmap bm = BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) YOUR_WIDTH) / width;
float scaleHeight = ((float) YOUR_HEIGHT) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
final Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width,
height, matrix, false);
runOnUiThread(new Runnable() {
@Override
public void run() {
your_imageView.setImageBitmap(resizedBitmap);
}
})
}
}).start();
}
谢谢。
【讨论】: