我尝试使用带有contentResolver的MediaStore.Images.Media提供的所有图像获取光标,但光标为空! (我在手机上试了很多图像)。代码是:
公共类MainActivity扩展了ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View arg0) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.INTERNAL_CONTENT_URI, filePathColumn, null,null,null);
if(cursor.moveToFirst()==false) Log.i("my message","EMPTY");
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
});
}
你知道如何至少有一个非空的游标吗?非常感谢
答案 0 :(得分:0)
使用EXTERNAL_CONTENT_URI
代替INTERNAL_CONTENT_URI
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, filePathColumn , null, null, null);
然后你将有一个包含所有文件路径的光标(MediaStore.Images.Media.DATA)