从内置存储Android手机上的画廊获取图像

您好我正在开发一个Android画廊应用程序,我从内置画廊获取图像,并显示它。我使用的代码如下

String[] projection = {MediaStore.Images.Thumbnails._ID}; Cursor cursor = getContentResolver().query(MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI, projection, // Which columns to return null, // Return all rows null, null); int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); int size = cursor.getCount(); // If size is 0, there are no images on the SD Card. if (size == 0) { Log.e("size 0","0"); } 

问题是,当我只有内部存储(Galaxy Nexus)的电话上运行此代码时,我得到的日志说,大小为零,即使有内置图库中的图像。 我如何解决这个问题。 请帮助。谢谢!

Solutions Collecting From Web of "从内置存储Android手机上的画廊获取图像"

试试你的button,如“浏览”

  browse.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent i = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, RESULT_LOAD_IMAGE); } }); 

你也cal设置选定的图像到你的ImageView

  protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); picturePath = cursor.getString(columnIndex); cursor.close(); imageView = (ImageView) findViewById(R.id.property_image); imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); } } 

在第一个代码块我使用startActivityForResult(i, RESULT_LOAD_IMAGE); 这个返回结果被称为活动,我们可以通过第二块代码获得这个结果,并在ImageView中设置选定的图像

 String[] projection = {MediaStore.Images.Media._ID}; 

MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URIreplace为MediaStore.Images.MEDIA.EXTERNAL_CONTENT_URI

  Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, // Which columns to return null, // Return all rows null, null); int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID); int size = cursor.getCount(); // If size is 0, there are no images on the SD Card. if (size == 0) { Log.e("size 0","0"); } 

编辑:获取缩略图列表,并从光标获取图像的URI


 Uri uri=MediaStore.Images.Thumbnails.getContentUri("external"); Cursor cursor=MediaStore.Images.Thumbnails.queryMiniThumbnails (getContentResolver(), uri, MediaStore.Images.Thumbnails.MINI_KIND,null); if( cursor != null && cursor.getCount() > 0 ) { String uri = cursor.getString( cursor.getColumnIndex( MediaStore.Images.Thumbnails.DATA ) ); } 

要获得图库列表,你可以试试这个

 String[] projection = new String[]{ MediaStore.Images.Media._ID, MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.Images.Media.DATE_TAKEN }; Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; Cursor cur = getContentResolver().query(imageUri, projection, // Which columns to return null, // Which rows to return (all rows) null, // Selection arguments (none) null // Ordering ); Log.i("Images Count"," count="+cur.getCount());