このコード、ファイルを探索します。Android 5、6 では問題なく動作しますが、android N では 3 回の受信エラーが発生します。どうすれば解決できますか?ありがとうございました。
コード:
public class MainActivity extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myPath = (TextView)findViewById(R.id.path);
root = Environment.getExternalStorageDirectory().getPath();
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
if(!file.isHidden() && file.canRead()) {
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
if(isVideo(file)){
item.add(file.getName());
}
}
}
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
public boolean isVideo(File file){
String filename = file.getName().toLowerCase();
String extension = filename.substring(filename.lastIndexOf("."), filename.length());
switch (extension) {
case ".3gp":
case ".mpg":
case ".mpeg":
case ".mpe":
case ".mp4":
case ".avi":
return true;
default:
return false;
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead()){
getDir(path.get(position));
}else{
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_folder_open_black_24dp)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK", null).show();
}
}else {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_folder_open_black_24dp)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK", null).show();
}
}
}
ログキャット:
04-09 17:52:20.830 11702-11702/ro.vrt.exploerfile E/AndroidRuntime: FATAL EXCEPTION: main
Process: ro.vrt.exploerfile, PID: 11702
java.lang.RuntimeException: Unable to start activity ComponentInfo{ro.vrt.exploerfile/ro.vrt.exploerfile.MainActivity}: java.lang.StringIndexOutOfBoundsException: length=7; index=-1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2581)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5849)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:763)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
Caused by: java.lang.StringIndexOutOfBoundsException: length=7; index=-1
at java.lang.String.substring(String.java:1903)
at ro.vrt.exploerfile.MainActivity.isVideo(MainActivity.java:73)
at ro.vrt.exploerfile.MainActivity.getDir(MainActivity.java:60)
at ro.vrt.exploerfile.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:6570)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5849)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:763)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
および問題のコード:
String extension = filename.substring(filename.lastIndexOf("."), filename.length());
if(isVideo(file)){
getDir(root);
以前のバージョンと N の間で大きな変更が行われるか、それとも現在安定していませんか?
編集:
私はこれを意図しています:
String extension = "";
String filename = file.getName().toLowerCase();
int i = filename.lastIndexOf('.');
if (i >= 0) {
extension = filename.substring(i -1);
}
/* String ext = null;
String s = file.getName();
int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1) {
ext = s.substring(i).toLowerCase();
}*/
switch (extension) {
case ".3gp":
case ".mpg":
case ".mpeg":
case ".mpe":
case ".mp4":
case ".avi":
case ".m3u8":
return true;
default:
return false;
}
androi N からの作業ですが、ファイル .mp4 .m3u8 .avi などは表示されません。