我正在寻找从SDCard文件夹中获取图像的代码,并创建一个活动中显示的图库。我在网上找到了一些消息来源,包括在这个论坛上,但我无法调整大小。
您能帮助我使用简单易用的源代码吗?
这是我想要它的方式,但从可绘制文件夹加载图像,我需要从SD卡文件夹。
http://javatechig.com/android/android-gridview-example-building-image-gallery-in-android
编辑 - 已添加代码
MainActivity.java
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0"
tools:context=".MainActivity" >
<GridView
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:drawSelectorOnTop="true"
android:stretchMode="columnWidth" >
</GridView>
</RelativeLayout>
row_grid.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:padding="5dp"
android:clickable="true"
android:background="@drawable/grid_color_selector"
android:focusable="true">
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp" >
</ImageView>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="12sp" >
</TextView>
</LinearLayout>
GridViewAdapter.java
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
*
* @author javatechig {@link http://javatechig.com}
*
*/
public class GridViewAdapter extends ArrayAdapter {
private Context context;
private int layoutResourceId;
private ArrayList data = new ArrayList();
public GridViewAdapter(Context context, int layoutResourceId,
ArrayList data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
ImageItem item = data.get(position);
holder.imageTitle.setText(item.getTitle());
holder.image.setImageBitmap(item.getImage());
return row;
}
static class ViewHolder {
TextView imageTitle;
ImageView image;
}
}
ImageItem.java
package com.javatechig.gridview;
import android.graphics.Bitmap;
/**
* @author javatechig {@link http://javatechig.com}
*
*/
public class ImageItem {
private Bitmap image;
private String title;
public ImageItem(Bitmap image, String title) {
super();
this.image = image;
this.title = title;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
MainActivity.java
import java.util.ArrayList;
import android.app.Activity;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.GridView;
/**
*
* @author javatechig {@link http://javatechig.com}
*
*/
public class MainActivity extends Activity {
private GridView gridView;
private GridViewAdapter customGridAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView);
customGridAdapter = new GridViewAdapter(this, R.layout.row_grid, getData());
gridView.setAdapter(customGridAdapter);
}
private ArrayList getData() {
final ArrayList imageItems = new ArrayList();
// retrieve String drawable array
TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
for (int i = 0; i < imgs.length(); i++) {
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
imgs.getResourceId(i, -1));
imageItems.add(new ImageItem(bitmap, "Image#" + i));
}
return imageItems;
}
}
谢谢!
答案 0 :(得分:1)
您可以获取手机图像目录的路径,执行以下操作:
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
由于您需要所有图像,因此可以通过调用listFiles()来获取文件数组 所以:
File[] imageFiles = path.listFiles();
要转换每个图像,您可以使用BitmapFactory,并迭代文件数组:
Bitmap image = BitmapFactory.decodeFile(imageFiles[i].getAbsolutePath());
所以在你的情况下,getData()方法看起来像这样:
private ArrayList getData() {
final ArrayList imageItems = new ArrayList();
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File[] imageFiles = path.listFiles();
for (int i = 0; i < imageFiles.length(); i++) {
Bitmap bitmap = BitmapFactory.decodeFile(imageFiles[i].getAbsolutePath())
imageItems.add(new ImageItem(bitmap, "Image#" + i));
}
return imageItems;}