单击按钮时,从库中的res / drawable-hdpi打开图像

时间:2013-12-31 09:24:26

标签: android image 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力会关闭。 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(),仍然是同样的错误。

任何建议?


  

在这里找到答案 -   How to open an image in drawable folder using android imageview applications?


1 个答案:

答案 0 :(得分:2)

这里是一个画廊活动的快速示例,如果这是你需要的。

   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个文件夹