图像从SD卡图库视图 [英] Image From sd card to galleryview

查看:154
本文介绍了图像从SD卡图库视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我想从SD卡中的图片到一个特定的位置,我需要在图库中显示,

In my app i want to get the Images from SD card to a specific position and i need to display in gallery,

在$ C C I使用的连接

The code i used is attaching

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)是图库视图或indefiniite一个漫长的没有尽头

1)How can i get all image in the specific folder in SD card
2) GalleryView is indefiniite or a long one without end

推荐答案

下面是在更新时间: code,以获得特定路径图片...

here is the UPDATED code to get images from specific path...

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

这篇关于图像从SD卡图库视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆