Android 加载系统图库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 加载系统图库相关的知识,希望对你有一定的参考价值。

public class MainActivity extends Activity implements LoaderCallbacks<Cursor>{

    private ListView listView=null;
    private Uri uri=null;
    private ContentResolver resolver=null;
    private CursorLoader cursorLoader=null;
    private String[] projection = new String[]{MediaStore.Images.Media._ID,MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
    private SimpleCursorAdapter cursorAdapter=null;
    private long[] ids = null;
    private LoaderManager loaderManager=null;
    
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);
        //获取媒体图片的uri
        uri=MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        resolver= getContentResolver();
        cursorLoader = new CursorLoader(this, uri, projection, null, null, null);
        cursorAdapter= new SimpleCursorAdapter(this, R.layout.item,null, projection, new int[]{R.id.textView_id,R.id.textView_name}, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        listView.setAdapter(cursorAdapter);
        //获取得到所有的条目的id
        ids=listView.getCheckedItemIds();
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                
                ids = listView.getCheckedItemIds();
                TextView textView_id = (TextView) view.findViewById(R.id.textView_id);
                TextView textView_name = (TextView) view.findViewById(R.id.textView_name);
                boolean flg = false;
                for (int i = 0; i < ids.length; i++) {
                    
                    if (id==ids[i]) {
                        
                        textView_id.setTextColor((Color.BLUE));
                        textView_name.setTextColor(Color.BLUE);
                        flg=true;
                    }
                    
                }
                
                if (!flg) {
                    textView_id.setTextColor((Color.BLACK));
                    textView_name.setTextColor(Color.BLACK);
                }
            }
        });
        
        loaderManager = getLoaderManager();
        loaderManager.initLoader(2, null, this);
        
    }



    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // TODO Auto-generated method stub
        return cursorLoader;
    }



    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        // TODO Auto-generated method stub
        cursorAdapter.swapCursor(data);
        
    }



    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        // TODO Auto-generated method stub
        cursorAdapter.swapCursor(null);
        
    }
    
    
}

 

以上是关于Android 加载系统图库的主要内容,如果未能解决你的问题,请参考以下文章

从图库中选择照片(onActivityResult()问题 - 我想是这样) - 片段Android

如何在片段中从相机捕获图像,

从相机/图库加载图像位图时会旋转 [Android 9]

Android:从图库加载的位图在 ImageView 中旋转

从图库中获取图像以在片段中的图像视图中设置? [复制]

开发图库视图时 setAdapter 出错 [重复]