得到位图从乌里[机器人]位图、机器人

2023-09-12 00:13:13 作者:浅夏初晴

如何从乌里得到位图对象(假设,如果我成功将其存储在 /data/data/MYFOLDER/myimage.png ///文件数据/数据​​/ MyFolder中/ myimage.png 我用两个路径),把它用在我的应用程序

how to get bitmap object from an Uri (suppose, if i succeed to store it in /data/data/MYFOLDER/myimage.png or file///data/data/MYFOLDER/myimage.png i used both paths) to use it in my application

但我没有得到这个回来。有没有人对如何做到这一点的想法?

but i am failing to get this back. Does anyone have an idea on how to accomplish this ?

推荐答案

。 。 重要:请参见从下面@马克英格拉姆回答和@pjv在更好的解决方案 。

您可以试试这个:

public Bitmap loadBitmap(String url)
{
    Bitmap bm = null;
    InputStream is = null;
    BufferedInputStream bis = null;
    try 
    {
        URLConnection conn = new URL(url).openConnection();
        conn.connect();
        is = conn.getInputStream();
        bis = new BufferedInputStream(is, 8192);
        bm = BitmapFactory.decodeStream(bis);
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
    finally {
        if (bis != null) 
        {
            try 
            {
                bis.close();
            }
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
        if (is != null) 
        {
            try 
            {
                is.close();
            }
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
    }
    return bm;
}

但要记住,此方法只应调用从一个线程(而不是GUI -thread)之内。我一个AsyncTask的。

But remember, this method should only be called from within a thread (not GUI -thread). I a AsyncTask.

 
精彩推荐
图片推荐