안드로이드에서 Uri로부터 비트맵을 추출하는 방법

조회수 2157회

Uri에서 비트맵 객체를 받아오려면 어떻게 해야하죠? 받아와서 비트맵을 저장할 경우 /data/data/MYFOLDER/myimage.png 나 file///data/data/MYFOLDER/myimage.png 이 두 경로를 사용할것 같은데

여러가지 방법을 시도해봤는데 안돼요. 아시는 분 계신가요?

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기

    이렇게 해보세요.

    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;
    }
    

    단, AsyncTask 같은 쓰레드로 돌리셔야됩니다.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)