繁体   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