2

在我的应用程序中,我想将图像从 SD 卡获取到特定位置,我需要在画廊中显示,

我使用的代码正在附加

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 是无限的或没有尽头的长

4

3 回答 3

6

这是从特定路径获取图像的更新代码...

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;
}
}
于 2013-01-09T07:29:47.207 回答
0

获取所有图像将导致内存溢出,最好的方法是以字符串形式保存路径列表并将其提供给画廊。如果您正在制作自定义画廊,则必须创建位图并进行垃圾收集。

这是访问 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-card 是否已加载

于 2013-01-09T07:52:16.290 回答
0
    //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);

}

}

于 2013-08-23T10:31:29.787 回答