从外部存储读取视频文件时出现 IllegalArgumentException

发布于 2024-12-13 14:52:45 字数 3618 浏览 0 评论 0原文

我有一个问题,从 SD 卡读取视频文件时遇到 IllegalArgumentException。我不知道为什么?请向我建议正确的解决方案。

错误堆栈:

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();
    }

I have a problem that I am getting IllegalArgumentException while reading a video file from sdcard. I don't know why? Please suggest me the right solution for the same.

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)

Code:

    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();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卷耳 2024-12-20 14:52:45

Context.openFileInput 不允许使用路径分隔符,因为它仅用于应用程序上下文中的私有文件。

http://developer.android.com/reference /android/content/Context.html#openFileInput(java.lang.String)

打开与此 Context 的应用程序包关联的私有文件进行读取。

您可以通过其他方式打开该文件(File 、FileInputStream 等),因为它位于sdcard,但您不能使用 openFileInput 来实现此目的。

Context.openFileInput will not allow path separators because it is only to be used for private files in the application's context.

http://developer.android.com/reference/android/content/Context.html#openFileInput(java.lang.String)

Open a private file associated with this Context's application package for reading.

You could open that file another way (File, FileInputStream, etc), since it is on the sdcard, but you can't use openFileInput for that purpose.

水染的天色ゝ 2024-12-20 14:52:45

我认为,此方法在应用程序的私有数据区域中打开一个文件。您无法打开该区域的子目录中的任何文件。因此,请使用 FileInputStream 等。

希望有帮助。

I think, this method opens a file in the private data area of application. You cannot open any files in subdirectories in this area . So use a FileInputStream or such.

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文