问题描述
我需要通过打开的InputStream如下获取图像我得到字节数据的URI之后创建从SD卡图像的位图,这样
I need to create a bitmap from sdcard images so after getting uri of image i am getting byte data by opening inputStream as follows
public byte[] getBytesFromFile(InputStream is)
{
byte[] data = null;
ByteArrayOutputStream buffer=null;
try
{
buffer = new ByteArrayOutputStream();
int nRead;
data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1)
{
buffer.write(data, 0, nRead);
}
buffer.flush();
return buffer.toByteArray();
} catch (IOException e)
{
return null;
}
finally
{
if(data!=null)
data = null;
if(is!=null)
try {
is.close();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if(buffer!=null)
{
try {
buffer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
buffer = null;
}
System.gc();
}
在这之后我只是按照code创建数据的位图
after this i am just creating bitmap of that data by following code
byte[] data = getBytesFromFile(is);
Bitmap bm= BitmapFactory.decodeByteArray(data, 0, data.length);
但它给了我OutOfMemory错误。许多人引导我检查内存泄漏,但是,这是我的应用程序的第一步,我的意思是直通意图过滤我开始从画廊的菜单选项共享这将调用我与图像URI应用我的应用程序..
PLZ指导我的家伙,如果我错了......也该异常来自哪个具有高分辨率的图像(大小excceds 1MB)的设备上,但任何应如何创建位图...
but it gives me outofmemory error. many peoples guide me to check for memory leakages but, this is the first step in my app., i mean thru intent filter i start my app from gallery menu option "share" which invokes my app with the image uri..plz guide me guys if i am wrong... and also this exception comes on the devices which have high resolution images(size excceds 1MB), but any how it should create Bitmap...
推荐答案
尝试使用方法,而不是先加载的ByteArray到内存中。还检查了该question有关加载位图的详细信息。
Try to use the BitmapFactory.decodeStream() method instead of first loading the bytearray into memory. Also check out this question for more info on loading bitmaps.
这篇关于如何创建针对Android的SD卡图像的位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!