开发者

How can I use Gallery to allow user to pick a resource from res/drawable in app?

I have previously used the android gallery in my app to allow user to select an image from the SD card (using the code below).

My question is: How can I do the same, but allow user to select image that is stored in my apps /res/drawable dir?

Code for using gallery to pick from SD:

Intent i = new Intent(Intent.ACTION_PICK,
               android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE); 

Then in onActivityForResult, call intent.getData() to get the Uri of the Image.

protected void onActivityResult(int requestCode, int resultCode, Intent imageRetu开发者_高级运维rnedIntent) { 
    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);
        }
    }
}


Although this isn't the best solution, Looks like I may have to write my own gallery view like this tutorial:

mgmblog.com/2008/12/12/listing-androids-drawable-resources

UPDATE: link is long dead, but here is the relevant code that allows me to iterate my apps 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();
    }
  }

Where com.example.appname is your apps namespace.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜