简体   繁体   中英

Open image from res/drawable-hdpi in gallery when clicking a button

i have been goin through various posts to find a way to open a image from my res folder, i have a button named share,i used the following code to do the same

Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri=Uri.parse("file:///android_asset/a.jpg");

tried fetching from aseets but image not loaded.

 Uri imgUri = Uri.parse("android.resource://"+getPackageName()+"/"     +"drawable/a");

the app force closes when i use this uri. intent.setDataAndType(Uri. parse("android.resource://com.example.hny_wallpaperset/" + R.drawable.a), "image/*"); app force closes

    intent.setDataAndType(uri, "image/*");
    startActivity(intent);

a is my image name,i also tried using my package name com.example.HNY_wallpaperset instead of getpackagename(),still same error.

any advice?


Found the answer here -- How to open an image in drawable folder using android imageview applications?


here is quick sample of one gallery activity if that is what you need anyway.

   public class ImageGalleryExample extends Activity implements
            AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);

            setContentView(R.layout.main);

            mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
            mSwitcher.setFactory(this);
            mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                    android.R.anim.fade_in));
            mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                    android.R.anim.fade_out));

            Gallery g = (Gallery) findViewById(R.id.gallery);
            g.setAdapter(new ImageAdapter(this));
            g.setOnItemSelectedListener(this);
        }

        public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
            mSwitcher.setImageResource(mImageIds[position]);
        }

        public void onNothingSelected(AdapterView<?> parent) {
        }

        public View makeView() {
            ImageView i = new ImageView(this);
            i.setBackgroundColor(0xFF000000);
            i.setScaleType(ImageView.ScaleType.FIT_CENTER);
            i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT));
            return i;
        }

        private ImageSwitcher mSwitcher;

        public class ImageAdapter extends BaseAdapter {
            public ImageAdapter(Context c) {
                mContext = c;
            }

            public int getCount() {
                return mThumbIds.length;
            }

            public Object getItem(int position) {
                return position;
            }

            public long getItemId(int position) {
                return position;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView i = new ImageView(mContext);

                i.setImageResource(mThumbIds[position]);
                i.setAdjustViewBounds(true);
                i.setLayoutParams(new Gallery.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                i.setBackgroundResource(R.drawable.picture_frame);
                return i;
            }

            private Context mContext;

        }

        private Integer[] mThumbIds = {
                R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
                R.drawable.sample_thumb_2, R.drawable.sample_thumb_3};

        private Integer[] mImageIds = {
                R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
                R.drawable.sample_3};

    }

but if you just want to set src to image view then use simple:

myImgView.setImageResource(R.drawable.sample_1);

btw, android itslef is choosing betwen drawable-?dpi folders

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM