簡體   English   中英

單擊按鈕時,從庫中的res / drawable-hdpi打開圖像

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

我已經通過各種帖子找到一種方法從我的res文件夾中打開圖像,我有一個名為share的按鈕,我用下面的代碼做同樣的事情

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

嘗試從aseets獲取但圖像未加載。

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

當我使用這個uri時,app force會關閉。 intent.setDataAndType(Uri. “android.resource://com.example.hny_wallpaperset/”+ R.drawable.a),“image / *”); app force關閉

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

a是我的圖像名稱,我也嘗試使用我的包名com.example.HNY_wallpaperset而不是getpackagename(),仍然是同樣的錯誤。

任何建議?


在這里找到答案 - 如何使用android imageview應用程序在drawable文件夾中打開圖像?


這里是一個畫廊活動的快速樣本,如果這是你需要的。

   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};

    }

但如果你只想將src設置為圖像視圖,那么使用簡單:

myImgView.setImageResource(R.drawable.sample_1);

順便說一句,android itslef正在選擇drawable-?dpi文件夾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM