我有一个问题,我在从SD卡读取视频文件时遇到IllegalArgumentException。我不知道为什么?请为我建议正确的解决方案。
ErrorStack:
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): FATAL EXCEPTION: main
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.TestCryptoActivity}: java.lang.IllegalArgumentException: File /mnt/sdcard/E0022505.mp4 contains a path separator
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at android.os.Looper.loop(Looper.java:123)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at java.lang.reflect.Method.invoke(Method.java:521)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at dalvik.system.NativeStart.main(Native Method)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): Caused by: java.lang.IllegalArgumentException: File /mnt/sdcard/E0022505.mp4 contains a path separator
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at android.app.ContextImpl.makeFilename(ContextImpl.java:1602)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at android.app.ContextImpl.openFileInput(ContextImpl.java:399)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at android.content.ContextWrapper.openFileInput(ContextWrapper.java:152)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at com.example.TestCryptoActivity.onCreate(TestCryptoActivity.java:29)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-03 18:56:18.733: ERROR/AndroidRuntime(24192): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
代码:
try {
is = this.openFileInput(Environment.getExternalStorageDirectory()+"/E0022505.mp4");
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[2097152];
try {
while ((bytesRead = is.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] bytes = bos.toByteArray();
try {
String byteString = new String(bytes,"UTF-8");
System.out.println("the bytes array of video:"+byteString);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
答案 0 :(得分:0)
我认为,此方法会在private data
应用程序区域中打开一个文件。您无法在此区域中的子目录中打开任何文件。所以使用FileInputStream
等。
希望它有所帮助。
答案 1 :(得分:0)
Context.openFileInput
将不允许路径分隔符,因为它仅用于应用程序上下文中的私有文件。
http://developer.android.com/reference/android/content/Context.html#openFileInput(java.lang.String)
打开与此Context的应用程序包关联的私有文件以供阅读。
您可以以另一种方式打开该文件(File,FileInputStream等),因为它位于SD卡上,但您不能将openFileInput用于此目的。