我正在创建一个应用程序,我需要读取SD卡图像。我想为IMAGE文件夹添加背景图片,如何设置? 如果需要更多信息,请回复
答案 0 :(得分:0)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Vector;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import com.androidpeople.tab.FindFilesByType.DownloadFileAsync;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ExpandableListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnGroupExpandListener;
public class SongList extends ExpandableListActivity {
private RingtoneAdapter expListAdapter;
public static SongList self;
int myProgress = 0;
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
ProgressBar myProgressBar;
ProgressDialog mProgressDialog;
List<String> items = new ArrayList<String>();
final Context myApp = this;
// private static final String DIRECTORY = "/system/media/audio/ringtones/";
private static final String DIRECTORY = "/sdcard/download/";
// private MediaPlayer mp = new MediaPlayer();
List<String> Ringtones = new ArrayList<String>();
ArrayAdapter<String> adapter;
TextView tv, empty;
ExpandableListView exlv1;
// ListView lv1;
Boolean hasErrors = false;
int currentPosition = 0;
private static final String LOG_TAG = "TEST";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
self=this;
setContentView(R.layout.main);
refreshList();
}
}
public void refreshList() {
File ringtones_directory = new File(DIRECTORY);
if (!ringtones_directory.exists()) {
AlertDialog.Builder ad = new AlertDialog.Builder(SongList.this);
ad.setTitle("Directory Not Found");
ad.setMessage("Sorry! The ringtones directory doesn't exist.");
ad.setPositiveButton("OK", null);
ad.show();
hasErrors = true;
}
if (!ringtones_directory.canRead()) {
AlertDialog.Builder ad = new AlertDialog.Builder(SongList.this);
ad.setTitle("Permissions");
ad
.setMessage("Sorry! You don't have permission to list the files in that folder");
ad.setPositiveButton("OK", null);
ad.show();
hasErrors = true;
} else {
Ringtones = FindFiles(false);
if (Ringtones.size() < 1) {
AlertDialog.Builder ad = new AlertDialog.Builder(SongList.this);
ad.setTitle("Permissions");
ad.setMessage("Sorry! No ringtones exists in " + DIRECTORY
+ ".");
ad.setPositiveButton("OK", null);
ad.show();
Log.e(LOG_TAG, "No ringtones were found.");
hasErrors = true;
}
}
try {
if (!hasErrors) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
SongList.this, android.R.layout.test_list_item,
Ringtones);
ArrayList<String> GrouppList = new ArrayList<String>();
GrouppList.addAll(Ringtones);
ArrayList<ArrayList<Rington>> colors = new ArrayList<ArrayList<Rington>>();
for (int i = 0; i <= Ringtones.size(); i++) {
ArrayList<Rington> color = new ArrayList<Rington>();
color = new ArrayList<Rington>();
color.add(new Rington("", "", true));
colors.add(color);
}
expListAdapter = new RingtoneAdapter(this, GrouppList, colors);
exlv1 = (ExpandableListView) findViewById(R.id.expandableListView1);
this.exlv1.setAdapter(this.expListAdapter);
}
} catch (Exception e) {
Log.i(LOG_TAG, e.toString());
}
}
private List<String> FindFiles(Boolean fullPath) {
final List<String> tFileList = new ArrayList<String>();
Resources resources = getResources();
// array of valid audio file extensions
String[] audioTypes = resources.getStringArray(R.array.audio);
FilenameFilter[] filter = new FilenameFilter[audioTypes.length];
int i = 0;
for (final String type : audioTypes) {
filter[i] = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith("." + type);
}
};
i++;
}
FileUtils fileUtils = new FileUtils();
File[] allMatchingFiles = fileUtils.listFilesAsArray(
new File(DIRECTORY), filter, -1);
for (File f : allMatchingFiles) {
if (fullPath) {
tFileList.add(f.getAbsolutePath());
} else {
tFileList.add(f.getName());
}
}
return tFileList;
}// find fil
@SuppressWarnings("unchecked")
public List<String> loadArray(String filename) {
try {
FileInputStream fis = new FileInputStream(filename);
GZIPInputStream gzis = new GZIPInputStream(fis);
ObjectInputStream in = new ObjectInputStream(gzis);
List<String> read_field = (List<String>) in.readObject();
in.close();
return read_field;
} catch (Exception e) {
e.getStackTrace();
}
return null;
}
public Collection<File> listFiles(File directory, FilenameFilter[] filter,
int recurse) {
Vector<File> files = new Vector<File>();
File[] entries = directory.listFiles();
if (entries != null) {
for (File entry : entries) {
for (FilenameFilter filefilter : filter) {
if (filter == null
|| filefilter.accept(directory, entry.getName())) {
files.add(entry);
Log.v(LOG_TAG, "Added: " + entry.getName());
}
}
if ((recurse <= -1) || (recurse > 0 && entry.isDirectory())) {
recurse--;
files.addAll(listFiles(entry, filter, recurse));
recurse++;
}
}
}
return files;
}
public class FileUtils {
public void saveArray(String filename, List<String> output_field) {
try {
FileOutputStream fos = new FileOutputStream(filename);
GZIPOutputStream gzos = new GZIPOutputStream(fos);
ObjectOutputStream out = new ObjectOutputStream(gzos);
out.writeObject(output_field);
out.flush();
out.close();
} catch (IOException e) {
e.getStackTrace();
}
}
public File[] listFilesAsArray(File directory, FilenameFilter[] filter,
int recurse) {
Collection<File> files = listFiles(directory, filter, recurse);
File[] arr = new File[files.size()];
return files.toArray(arr);
}
}
}
以上代码将列出以下所有包含来自SD卡的扩展文件,您可以根据您的要求更改它们快乐编码:Pragna
这是res
文件夹
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="audio">
<item>aif</item>
<item>aifc</item>
<item>aiff</item>
<item>au</item>
<item>mid</item>
<item>mp3</item>
<item>ogg</item>
<item>snd</item>
<item>wav</item>
</string-array>
</resources>
答案 1 :(得分:0)
public class Images extends Activity
{
private Uri[] mUrls;
String[] mFiles=null;
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.images);
File images = Environment.getDataDirectory();
File[] imagelist = images.listFiles(new FilenameFilter(){
@override
public boolean accept(File dir, String name)
{
return ((name.endsWith(".jpg"))||(name.endsWith(".png"))
}
});
mFiles = new String[imagelist.length];
for(int i= 0 ; i< imagelist.length; i++)
{
mFiles[i] = imagelist[i].getAbsolutePath();
}
mUrls = new Uri[mFiles.length];
for(int i=0; i < mFiles.length; i++)
{
mUrls[i] = Uri.parse(mFiles[i]);
}
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setFadingEdgeLength(40);
}
public class ImageAdapter extends BaseAdapter{
int mGalleryItemBackground;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount(){
return mUrls.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 i = new ImageView(mContext);
i.setImageURI(mUrls[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(260, 210));
return i;
}
private Context mContext;
}
}