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
    //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 に答える
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カードがロードされていることを確認する必要があります

于 2013-01-09T07:52:16.290 に答える