I am trying to get all images in my assets folder using the codes below
private List<String> getImage()
{
/* 设定目前所在路径 */
List<String> it=new ArrayList<String>();
File f=new File("file:///android_asset/");
File[] files=f.listFiles();
Log.d("tag", "读取asset资源");
/* 将所有文件存入ArrayList中 */
for(int i=0;i<files.length;i++)
{
File file=files[i];
if(getImageFile(file.getPath()))
it.add(file.getPath());
}
return it;
}
private boolean getImageFile(String fName)
{
boolean re;
/* 取得扩展名 */
String end=fName.substring(fName.lastIndexOf(".")+1,
fName.length()).toLowerCase();
/* 按扩展名的类型决定MimeType */
if(end.equals("jpg")||end.equals("gif")||end.equals("png")
||end.equals("jpeg")||end.equals("bmp"))
{
re=true;
}
else
{
re=false;
}
return re;
}
somehow I am not sure about this expression
File f=new File("file:///android_asset/");
Well, I know that when read a txt file or html file from assets folder you can use this,but would images accept this too?
I have resolve this problem by using the code below
private List<String> getImage() throws IOException
{
AssetManager assetManager = getAssets();
String[] files = assetManager.list("image");
List<String> it=Arrays.asList(files);
return it;
}
in case somebody else want to know
Right click on the assets folder, select New >> file (myText. txt) and your text. “Darkness cannot drive out darkness: only light can do that. Hate cannot drive out hate: only love can do that.”
If you cannot open your ASSETS file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a ASSETS file directly in the browser: Just drag the file onto this browser window and drop it.
To List all files in directory from internal storage first we will fetch all the files and folder by using below code and then pass this data to GridView. builder() to show the files. To delete file/folder we will use the below code.
Assets provide a way to add arbitrary files like text, XML, HTML, fonts, music, and video in the application. If one tries to add these files as “resources“, Android will treat them into its resource system and you will be unable to get the raw data.
In general, you can use the AssetManager to manage files in your asset folder. In the activity, call getAssets () method to get an AssetManager Instance.
EDIT: you can use the AssetManager to read images like this:
AssetManager am=this.getAssets();
try {
Bitmap bmp=BitmapFactory.decodeStream(am.open("009.gif"));
chosenImageView.setImageBitmap(bmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Note that the BitmapFactory.decodeStream()
method runs on UI thread by default, so the app would get stuck if the image is too large. In that case, you can change the samplesize or start a new thread to do this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With