当前位置: 代码迷 >> Android >> 怎么调用系统gallery打开指定文件夹中的图片
  详细解决方案

怎么调用系统gallery打开指定文件夹中的图片

热度:24   发布时间:2016-05-01 21:40:41.0
如何调用系统gallery打开指定文件夹中的图片
如何调用系统gallery打开指定文件夹中的图片???

------解决方案--------------------
1.
Java code
File file=new File("/sdcard/IMG/1.jpg");                                                Intent it = new Intent(Intent.ACTION_VIEW);                Uri mUri = Uri.parse("file://"+file.getPath());                it.setDataAndType(mUri, "image/*");                startActivity(it);
------解决方案--------------------
Java code
<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />    <Gallery        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello"        android:id="@+id/gallery_id"        android:layout_x="43px"           android:layout_y="142px" /></LinearLayout>
------解决方案--------------------
Java code
package com.yezijp.gallerytest;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class GalleryTestActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                Gallery gallery = (Gallery) this.findViewById(R.id.gallery_id);        gallery.setAdapter(new GalleryAdapter(this));    }        public class GalleryAdapter extends BaseAdapter {                private Context context;                private int[] myImages = {                R.drawable.a1,                 R.drawable.a2,                 R.drawable.a3,                R.drawable.a4};                public GalleryAdapter (Context c) {            this.context = c;        }        @Override        public int getCount() {            return myImages.length;        }        @Override        public Object getItem(int position) {            return position;        }        @Override        public long getItemId(int position) {            return position;        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            ImageView imageView = new ImageView(this.context);            imageView.setImageResource(this.myImages[position]);            imageView.setScaleType(ImageView.ScaleType.FIT_XY);            imageView.setLayoutParams(new Gallery.LayoutParams(120, 120));            return imageView;        }            }}
------解决方案--------------------
主要就是在布局文件里加上Gallery控件
之后关联控件并设置适配器
Gallery gallery = (Gallery) this.findViewById(R.id.gallery_id);
gallery.setAdapter(new GalleryAdapter(this));

之后在自定义适配器GalleryAdapter中设置图片资源的数组
imageView.setImageResource(this.myImages[position]);
------解决方案--------------------
BitmapFactory.Options options=new Options();
options.inSampleSize=0;
Bitmap bitmap=BitmapFactory.decodeFile("图片文件路径",options);
ImageView imgView=new ImageView(context);
imgView.setScaleType(ImageView.ScaleType.FIT_XY);