Android 位图工厂示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11182714/
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
Bitmapfactory example
提问by nelzkie
I want to create a dynamic image view where every image in my gallery will going to use bitmapfactory and not image drawable that binds in the image view. Is there some sites that has bitmapfactory tutorial for this? i believe that using bitmapfactory uses less memory that binding the image into image view? Is this right? I want also to minimize the risk of memory leaks thats why I want to use bitmapfactory. Please help. I cant find basic examples that teaches bitmapfactory.
我想创建一个动态图像视图,其中我画廊中的每个图像都将使用 bitmapfactory 而不是绑定在图像视图中的图像可绘制。是否有一些网站为此提供位图工厂教程?我相信使用 bitmapfactory 使用更少的内存将图像绑定到图像视图中?这是正确的吗?我还想最大限度地减少内存泄漏的风险,这就是我想使用位图工厂的原因。请帮忙。我找不到教授位图工厂的基本示例。
回答by K_Anas
Building Bitmap Objects
构建位图对象
1) From a File
1)从文件
Use the adb tool with push option to copy test2.png onto the sdcard
使用带有 push 选项的 adb 工具将 test2.png 复制到 sdcard
This is the easiest way to load bitmaps from the sdcard. Simply pass the path to the image to BitmapFactory.decodeFile() and let the Android SDK do the rest.
这是从 SD 卡加载位图的最简单方法。只需将图像路径传递给 BitmapFactory.decodeFile(),然后让 Android SDK 完成剩下的工作。
public class TestImages extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.test_image);
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png");
image.setImageBitmap(bMap);
}
}
All this code does is load the image test2.png that we previously copied to the sdcard. The BitmapFactory creates a bitmap object with this image and we use the ImageView.setImageBitmap()
method to update the ImageView component.
这段代码所做的就是加载我们之前复制到 SD 卡的图像 test2.png。BitmapFactory 使用此图像创建一个位图对象,我们使用该ImageView.setImageBitmap()
方法更新 ImageView 组件。
2) From an Input stream
2)来自输入流
Use BitmapFactory.decodeStream()
to convert a BufferedInputStream into a bitmap object.
使用BitmapFactory.decodeStream()
到的BufferedInputStream转换成位图对象。
public class TestImages extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.test_image);
FileInputStream in;
BufferedInputStream buf;
try {
in = new FileInputStream("/sdcard/test2.png");
buf = new BufferedInputStream(in);
Bitmap bMap = BitmapFactory.decodeStream(buf);
image.setImageBitmap(bMap);
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
}
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
}
}
This code uses the basic Java FileInputStream and BufferedInputStream to create the input stream for BitmapFactory.decodeStream()
. The file access code should be surrounded by a try/catch block to catch any exceptions thrown by FileInputStream or BufferedInputStream. Also when you're finished with the stream handles they should be closed.
此代码使用基本的 Java FileInputStream 和 BufferedInputStream 为BitmapFactory.decodeStream()
. 文件访问代码应该被 try/catch 块包围,以捕获 FileInputStream 或 BufferedInputStream 抛出的任何异常。此外,当您完成流句柄时,它们应该关闭。
3) From your Android project's resources
3)从你的 Android 项目的资源
Use BitmapFactory.decodeResource(res, id)
to get a bitmap from an Android resource.
用于BitmapFactory.decodeResource(res, id)
从 Android 资源获取位图。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.test_image);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
image.setImageBitmap(bMap);
}