if ((new File("file://android_assets/aa.mp3")).exists()) {}
if ((new File("aa.mp3")).exists()) {}
以上方法都无法判断
感谢以上两位朋友的回答,@franzhong的方式我用过,也是可以的
下面我用的方法,
/**
* 加载本地图片
* @param url
* @return
*/
public static Bitmap getLoacalBitmapByAssets(Context c, String url)
{
Bitmap bitmap = null;
InputStream in = null;
try
{
in = c.getResources().getAssets().open(url);
bitmap = BitmapFactory.decodeStream(in);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
closeStream(in, null);
}
return bitmap;
}
Bitmap bmp = getLoacalBitmapByAssets(getActivity(), strIconName);
if (bmp!=null){ //如果返回的bmp不为空,表示存在这这个资源
ivIcon.setImageBitmap(bmp);
//I
AssetManager asset = getAssets();
try {
asset.open("filename");
} catch (IOException e) {
e.printStackTrace();
}
如果异常就代表不存在吧
private boolean bAssetsFile(String pt){
AssetManager am = getAssets();
try {
String[] names = am.list("");
for(int i=0;i<names.length;i++){
//Log.d("FILES",names[i]);
if(names[i].equals(pt.trim())){
System.out.println(pt+"文件存在!!!!");
return true;
}else{
//System.out.println(pt+"不存在啦!!!!");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("不存在!");
return false;
}
//call the function
//bAssetsFile("blue_three.png");
默认访问assets根目录
学习,谢谢·franzhong 。
如果判断APP包内对应的File文件夹下的某个文件是否存在,如何判别呢?