简体   繁体   English

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

[英]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 我已经通过各种帖子找到一种方法从我的res文件夹中打开图像,我有一个名为share的按钮,我用下面的代码做同样的事情

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. 尝试从aseets获取但图像未加载。

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

the app force closes when i use this uri. 当我使用这个uri时,app force会关闭。 intent.setDataAndType(Uri. parse("android.resource://com.example.hny_wallpaperset/" + R.drawable.a), "image/*"); intent.setDataAndType(Uri. “android.resource://com.example.hny_wallpaperset/”+ R.drawable.a),“image / *”); app force closes app force关闭

    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. a是我的图像名称,我也尝试使用我的包名com.example.HNY_wallpaperset而不是getpackagename(),仍然是同样的错误。

any advice? 任何建议?


Found the answer here -- How to open an image in drawable folder using android imageview applications? 在这里找到答案 - 如何使用android imageview应用程序在drawable文件夹中打开图像?


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: 但如果你只想将src设置为图像视图,那么使用简单:

myImgView.setImageResource(R.drawable.sample_1);

btw, android itslef is choosing betwen drawable-?dpi folders 顺便说一句,android itslef正在选择drawable-?dpi文件夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何打开JPG文件作为保存在…/ res / drawable-hdpi /中的BITMAP? - How to open a JPG file as a BITMAP stored on the …/res/drawable-hdpi/? WebView如何获取本地res \\ drawable-hdpi图像路径 - WebView how to get local res\drawable-hdpi image path Android 资源问题:如何获取 res/drawable-hdpi 文件夹的标识符? - Android resources problem: How to getIdentifier for a res/drawable-hdpi folder? 以hdpi分辨率为图像命名drawable-hdpi重要吗? - is it important to name drawable-hdpi for image in hdpi resolution? 从/ drawable-hdpi而不是/ drawable加载为背景时,不同大小的Drawable - Different size of Drawable when loaded as background from /drawable-hdpi instead of /drawable 如何同时使用可绘制和可绘制hdpi中的资源? - how to use resources from drawable and drawable-hdpi at the same time? 从URL下载图像并将其保存在我的应用程序的drawable-hdpi文件夹中 - Download image from URL and save it in my app's drawable-hdpi folder 如何使用res文件夹的drawable-hdpi,drawable-mdpi,drawable-ldpi中存在的图像? - how to use images present in drawable-hdpi,drawable-mdpi,drawable-ldpi of res folder? Android:如何在drawable-hdpi中放置具有不同分辨率的相同图像 - Android: how to place same image with different resoultion in drawable-hdpi 图像大小(drawable-hdpi/ldpi/mdpi/xhdpi) - image size (drawable-hdpi/ldpi/mdpi/xhdpi)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM