2016-12-05 6 views
0

私はAndroidの簡単なGridViewを使って写真ライブラリの画像を表示しています。例えばAndroidのGridViewの画像のパス

:私のコードは、画像のパスを取得することができます

public class ImageAdapter extends BaseAdapter { 

    private Context mContext; 

    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    public int getCount() { 
     return mThumbIds.length; 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

    // create a new ImageView for each item referenced by the Adapter 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 
     if (convertView == null) { 
      // if it's not recycled, initialize some attributes 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8, 8, 8, 8); 
     } else { 
      imageView = (ImageView) convertView; 
     } 

     imageView.setImageResource(mThumbIds[position]); 
     return imageView; 
    } 

    // references to our images 
    private Integer[] mThumbIds = { 
      R.drawable.face, R.drawable.face2, 
      R.drawable.face3, R.drawable.face4, 
      R.drawable.face5, R.drawable.face6, 
      R.drawable.face7, R.drawable.face8 
    }; 
} 

私は絵を使いたい:ストレージ//0/DCIM /カメラ/ IMG_20161205_101211.jpg

マイGridViewアダプタをエミュレートパスをmThumbIdsで表示する代わりに、画像を表示するにはassetsフォルダ内の画像を使用しますが、実際にその方法を知っているわけではありません。画像パスをBitmapに変換する必要がありますか?

+1

使用のような一ピカソなど(https://android-arsenal.com/tag/46)、[Androidのために利用可能な画像ローディングライブラリの一見無限の数]。これらはあなたの画像を 'ImageView'に非同期的に読み込むことができるので、画像を読み込む行為はメインのアプリケーションスレッドを束縛せずUIをフリーズします。 – CommonsWare

答えて

0

使用このコード

try { 
    // get input stream put name here 
    InputStream ims = getAssets().open("avatar.jpg"); 
    // load image as Drawable 
    Drawable d = Drawable.createFromStream(ims, null); 
    // set image to ImageView 
    imageView.setImageDrawable(d); 
} 
catch(IOException ex) { 
    return; 
関連する問題