如题。
/mnt/sdcard/Cache/?.jpg
/mnt/sdcard/Cache/?.jpg
/mnt/sdcard/Cache/?.jpg以上是已经存入内存卡中的文件。上列?号的意思是,并不能预先知道图片的文件名。如果能够读取到以上路径中的三张图片?(不一定是三张,也有可能是四张,五张。不固定)小弟初学,谢谢大家指教。谢谢~
/mnt/sdcard/Cache/?.jpg
/mnt/sdcard/Cache/?.jpg
/mnt/sdcard/Cache/?.jpg以上是已经存入内存卡中的文件。上列?号的意思是,并不能预先知道图片的文件名。如果能够读取到以上路径中的三张图片?(不一定是三张,也有可能是四张,五张。不固定)小弟初学,谢谢大家指教。谢谢~
File fileList[] = f.listFiles();
int length = fileList.length;
for (int i =0; i < length; i++)
{
//获取每个文件
File file=fileList[i];
............
}
我按照楼上教的方法,自己写了下。
/**
* 读取存储卡中图片
* @return
*/
public ArrayList<Bitmap> get_SdcardIMG()
{
ArrayList<Bitmap> aList = null;
try {
File file = new File("/mnt/sdcard/Cache/");
File fileList[] = file.listFiles();
int length = fileList.length;
for (int i =0; i < length; i++)
{
//获取每个文件
File fileNum = fileList[i];
aList.add((Bitmap)fileNum);
}
} catch (Exception e) {
e.printStackTrace();
}
return aList;
}但是怎么把得到的文件转换为图片呢?
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.support.v4.app.NavUtils;public class MainActivity extends Activity {
// 文件路径
private static ArrayList<String> paths = new ArrayList<String>(); @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView1 = (ImageView) findViewById(R.id.ImagetView1);
Map<String, Bitmap> maps = new TreeMap<String, Bitmap>();
maps = get_SdcardIMG("/mnt/sdcard/Cache/");
Iterator<String> it = maps.keySet().iterator();
int i = 0;
imageView1.setImageBitmap(maps.get("0"));
// while (it.hasNext()) {
// String path = (String) it.next();
// Bitmap bm = maps.get(path);
// imageView1.setImageBitmap(bm);
// }
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
} /**
* 读取存储卡中图片
*
* @return
*/
public Map<String, Bitmap> get_SdcardIMG(String path) {
Map<String, Bitmap> maps = null;
try {
// path = "/mnt/sdcard/Cache/"
File baseFile = new File(path);
// 使用TreeMap,排序问题就不需要纠结了
maps = new TreeMap<String, Bitmap>();
if (baseFile != null && baseFile.exists()) {
paths = imagePath(baseFile);
if (!paths.isEmpty()) {
for (int i = 0; i < paths.size(); i++) {
Bitmap bitmap = BitmapFactory.decodeFile(paths.get(i));
// maps.put(paths.get(i), bitmap);
maps.put(String.valueOf(i), bitmap);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return maps;
} /**
* 获取图片地址列表
*
* @param file
* @return
*/
public static ArrayList<String> imagePath(File file) {
ArrayList<String> list = new ArrayList<String>(); File[] files = file.listFiles();
for (File f : files) {
list.add(f.getAbsolutePath());
}
// Collections.sort(list);
return list;
}
}