Android 从sd卡到galleryview的图像

Android 从sd卡到galleryview的图像,android,android-sdcard,Android,Android Sdcard,在我的应用程序中,我想将图像从SD卡移到特定位置,并需要在gallery中显示 我使用的代码是附加的 private Gallery gallery; private ImageView imgView; int position; private byte[] data = { }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s

在我的应用程序中,我想将图像从SD卡移到特定位置,并需要在gallery中显示

我使用的代码是附加的

private Gallery gallery;
private ImageView imgView;
int position;
private byte[] data = { };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gridview);
      try{

        String filepath = "/sdcard/data/Crak/";
        File imagefile = new File(filepath +"abcd.jpg" );
        FileInputStream fis = new FileInputStream(imagefile);
        Bitmap bi = BitmapFactory.decodeStream(fis);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        data = baos.toByteArray();
        System.out.println("cnvrtn");
    }
    catch(Exception e) {
         e.printStackTrace();

    }

    final Bitmap bitmapimage = BitmapFactory.decodeByteArray(data, 0, data.length);
    position = 0;
    imgView = (ImageView) findViewById(R.id.ImageView01);
    imgView.setImageBitmap(bitmapimage);

    gallery = (Gallery) findViewById(R.id.examplegallery);
    gallery.setAdapter(new AddImgAdp(this));

    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position,
                long id) {
            imgView.setImageBitmap(bitmapimage);
            DisplayImage.this.position = position;
        }
    });
    System.out.println("Enter the activity//////////");

    imgView.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")


        @Override
        public void onClick(View paramView) {
            // TODO Auto-generated method stub

        }
    });

}

public class AddImgAdp extends BaseAdapter {
    int GalItemBg;
    private Context cont;

    @SuppressWarnings("null")
    public AddImgAdp(Context c) {
        cont = c;

        TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
        GalItemBg = typArray.getResourceId(
                R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        typArray.recycle();
    }

    public int getCount() {
        return data.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 imgView = new ImageView(cont);
        final Bitmap bitmapimage = BitmapFactory.decodeByteArray(data, 0, data.length);
        imgView.setImageBitmap(bitmapimage);;
        imgView.setLayoutParams(new Gallery.LayoutParams(100, 100));
        imgView.setScaleType(ImageView.ScaleType.FIT_XY);
        imgView.setBackgroundResource(GalItemBg);

        return imgView;

    }
}
问题是

1) 如何获取SD卡中特定文件夹中的所有图像

2) GalleryView是不确定的或是没有尽头的长视图

这里是更新的代码,用于从特定路径获取图像

package com.example.gall;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.example.gall.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

public class gall extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this, ReadSDCard()));

g.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent,
      View v, int position, long id) {
    }
});
}

private List<String> ReadSDCard()
{
 List<String> tFileList = new ArrayList<String>();

 //It have to be matched with the directory in SDCard
 File f = new File("/android/sdcard2/");

 File[] files=f.listFiles();

 for(int i=0; i<files.length; i++)
 {
  File file = files[i];
  /*It's assumed that all file in the path are in supported type*/
  tFileList.add(file.getPath());
 }
 return tFileList;
}

public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private List<String> FileList;

public ImageAdapter(Context c, List<String> fList) {
    mContext = c;
    FileList = fList;
    TypedArray a = obtainStyledAttributes(R.styleable.gall);
    mGalleryItemBackground = a.getResourceId(
    R.styleable.gall_android_galleryItemBackground,0);
    a.recycle();
}

public int getCount() {
    return FileList.size();
}

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

    Bitmap bm = BitmapFactory.decodeFile(
      FileList.get(position).toString());
    i.setImageBitmap(bm);

    i.setLayoutParams(new Gallery.LayoutParams(150, 100));
    i.setScaleType(ImageView.ScaleType.FIT_XY);
    i.setBackgroundResource(mGalleryItemBackground);

    return i;
}
}

public TypedArray obtainStyledAttributes(int theme) {
// TODO Auto-generated method stub
return null;
}
}
package com.example.gall;
导入java.io.File;
导入java.util.ArrayList;
导入java.util.List;
导入com.example.gall.R;
导入android.app.Activity;
导入android.content.Context;
导入android.content.res.TypedArray;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.os.Bundle;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.BaseAdapter;
导入android.widget.Gallery;
导入android.widget.ImageView;
导入android.widget.AdapterView.OnItemClickListener;
公务舱胆扩大活动范围{
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
画廊g=(画廊)findViewById(R.id.Gallery);
g、 setAdapter(新的ImageAdapter(此为ReadsCard());
g、 setOnItemClickListener(新的OnItemClickListener(){
公共无效MClick(AdapterView父级、,
视图v,内部位置,长id){
}
});
}
私人列表读卡器()
{
List-tFileList=new-ArrayList();
//它必须与SD卡中的目录匹配
文件f=新文件(“/android/sdcard2/”);
File[]files=f.listFiles();

对于(int i=0;i获取所有图像将导致内存溢出,最好的方法是以字符串形式保留路径列表并将其提供给库。如果要创建自定义库,则必须创建位图并对其进行垃圾收集

这是访问sd卡的好方法:

String path = Environment.getExternalStorageDirectory().getPath()+ "/yourFolder/"
这是如何获取列表的

File directory = new File(path);

if(directory.isDirectory)
{
    if(directory.list().length > 0 /*&& check if its an img etc*/)
    {
        for(String s: directory.list())
        {
            Log.d(TAG, "adding " + s);

            fileNames.add(s);
        }
    }
}
您应该确保检查sd卡是否已加载

    //YOUR CODE HERE... 
String[] projection = new String[]{
        MediaStore.Images.Media._ID,
        MediaStore.Images.Media.DATA, // add DATA column
        MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
        MediaStore.Images.Media.DATE_TAKEN,
        MediaStore.Images.Media.TITLE,

};
// Get the base URI for the People table in the Contacts content provider.
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Log.i("URI", images.toString());

        // Make the query.
Cursor cur = managedQuery(
        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
        projection, // Which columns to return
        null,         // Which rows to return (all rows)
        null,       // Selection arguments (none)
        MediaStore.Images.Thumbnails.IMAGE_ID          // Ordering
        );

Log.i("ListingImages"," query count="+cur.getCount());

if (cur.moveToFirst()) {
    String bucket;
    String date;
    String name;


    int bucketColumn = cur.getColumnIndex(
        MediaStore.Images.Media.BUCKET_DISPLAY_NAME);

    int dateColumn = cur.getColumnIndex(
        MediaStore.Images.Media.DATE_TAKEN);

    int nameColumn = cur.getColumnIndex(
            MediaStore.Images.Media.TITLE);


        // Get the field values
        bucket = cur.getString(bucketColumn);
        date = cur.getString(dateColumn);
        name = cur.getString(nameColumn);
      int columnIndex = cur.getColumnIndex(MediaStore.Images.Media.DATA);
      String picPath = cur.getString(columnIndex);

         imageView.setImageBitmap(BitmapFactory.decodeFile(picPath));
        // Do something with the values.
        Log.i("ListingImages", " bucket=" + bucket 
               + "  name_taken=" + name);

}

}

您想要所有图像还是要到特定文件夹…?仅从特定文件夹
GalleryView是不确定的,或者是一个没有结尾的长文件夹
=>那么您想要什么?您不能在这里分页。如果我的文件夹有5个图像意味着我的GalleryView应该只包含5个图像,但它有许多字段有重复的图像..@poovizir阿詹,我的pleasure@SilentKiller如何在gallery启动时在前面设置特定的
图像