2011-09-12 4 views
0

갤러리에서 이미지를 검색하고 있습니다. 지금은 onDraw(Canvas canvas)에 이미지를 표시하고 싶습니다. 할 수 있습니다. 친절하게 도와주세요. 사전에 감사uri 이미지를 캔버스 ondraw 메서드로 변환하는 방법

다음
selectedImageUri = data.getData(); 
         selectedImagePath = getPath(selectedImageUri); 
         Toast.makeText(getBaseContext(),"selected"+selectedImagePath,Toast.LENGTH_LONG).show(); 
         System.out.println("Image Path : " + selectedImagePath); 
         img.setImageURI(selectedImageUri); 

URI selectedImageUri;

OnDraw(canvas Canvas) 코드 :

Bitmap myBitmap1 = BitmapFactory.decodeResource(getResources(),selectedImageUri); 

내 오류 메시지 유형 BitmapFactory의 방법 decodeResource (자원, INT)는 인수 (자원, 열린 우리당)

적용되지 않습니다

답변

1

피커에서 다시 얻은 경로는 Uri이며,이를 리소스 ID 인 int로로드하려고합니다. getData()에서 반환 된 경로는 SD 카드의 파일에 직접 연결된 파일 경로이거나 MediaStore Uri입니다. 앱이 파일을 디스크에 저장하고 MediaStore API 메소드를 사용하여 MediaStore 데이터베이스에 삽입하지 않으면 파일 경로가 생성됩니다. 그렇지 않으면 당신은 MediaStore Uri를 얻습니다. 이러한 이유로, 나는 그것이 어떤 결정하고 실제 경로를 반환하는 래퍼 방법을 사용하십시오

public static String getRealPathFromURI(Activity activity, Uri contentUri) {  


    String realPath = null; 

    // Check for valid file path 
    File f = new File(contentUri.getPath()); 
    if(f.exists()) 
     realPath = contentUri.getPath(); 
    // Check for valid MediaStore path 
    else 
    {   
     String[] proj = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null); 
     if(cursor != null) 
     { 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      realPath = cursor.getString(column_index); 
      cursor.close(); 
     } 
    } 
    return realPath; 
} 

난 BitmapFactory에서 스트림으로로드 것을 일단 :

주의 많은 코드가 누락되어 있으므로 누락 된 부분이있을 수 있지만 일반적인 접근 방식을 사용해야합니다.

FileInputStream in = null; 
    BufferedInputStream buffer = null; 
    Bitmap image = null; 

    try 
    { 
     in = new FileInputStream(path); 
     buffer = new BufferedInputStream(in); 
     image = BitmapFactory.decodeStream(buffer); 
    } 
    catch (FileNotFoundException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    finally 
    { 
     try 
     { 
      if(in != null) 
       in.close(); 
     } 
     catch (IOException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try 
     { 
      if(buffer != null) 
       buffer.close(); 
     } 
     catch (IOException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 
관련 문제