我之前在我的应用程序中使用了android库,允许用户从SD卡中选择图像(使用下面的代码)。
我的问题是:我怎样才能这样做,但允许用户选择存储在我的apps / res / drawable目录中的图片?
使用图库从SD中选择的代码:
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
然后在onActivityForResult中,调用intent.getData()来获取Image的Uri。
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case REQ_CODE_PICK_IMAGE:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
}
}
}
答案 0 :(得分:1)
虽然这不是最佳解决方案,但看起来我可能需要编写自己的图库视图,如本教程:
mgmblog.com/2008/12/12/listing-androids-drawable-resources
更新:链接已经过时了,但这里是相关代码,允许我迭代我的应用程序res drawables:
Field[] drawables = com.example.appname.R.drawable.class.getFields();
for (Field f : drawables) {
try {
System.out.println("com.example.appname.R.drawable." + f.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
com.example.appname是您的应用程序命名空间。