2011-09-12 6 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からストリームとして、それを読み込むことをしたら:

NOTEのたくさんコードはここでは省略されていますので、何か不足している可能性がありますが、これは一般的なアプローチになります。

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(); 
     } 
    } 
} 
関連する問題