android error (1, -2147483648),Android MediaPlayer/VideoView error (1, -2147483648)

As it turns out, error -2147483648 indicates an unknown error. This could have something to do with the video encoding, but it's also worth checking that the file path exists and that the VideoView has permission to read it.

My issue was that I was writing my files with Context.MODE_PRIVATE (the default).

openFileOutput(filename, Context.MODE_PRIVATE);

This indicates that only your application can access the file. I don't know specifically how or why, but in Jelly Bean and above, it appears that the video view is allowed to access the file you specify as if it were your application, but before Jelly Bean, the video view tries to open the file in its own context (not your application's). Since the mode is private, it fails.

One solution is to write your file with Context.MODE_WORLD_READABLE, which is now deprecated. This indicates that anyone can open the file at that path. This is obviously unsafe and discouraged.

I ended up creating a content provider and my own URI to handle this case. Specifically:

AndroidManfest.xml:

...

android:name="com.myexampleapp.video.VideoProvider"

android:authorities="com.myexampleapp.video.VideoProvider.files"

android:exported="false" />

VideoProvider.java:

package com.myexampleapp.video;

import java.io.File;

import java.io.FileNotFoundException;

import android.content.ContentProvider;

import android.content.ContentValues;

import android.database.Cursor;

import android.net.Uri;

import android.os.ParcelFileDescriptor;

public class VideoProvider extends ContentProvider {

public static final Uri CONTENT_URI_BASE =

Uri.parse("content://com.myexampleapp.video.VideoProvider.files.files/");

private static final String VIDEO_MIME_TYPE = "video/mp4";

@Override

public boolean onCreate() {

return true;

}

@Override

public String getType(final Uri uri) {

return VIDEO_MIME_TYPE;

}

@Override

public ParcelFileDescriptor openFile(final Uri uri, final String mode)

throws FileNotFoundException {

File f = new File(uri.getPath());

if (f.exists())

return (ParcelFileDescriptor.open(f,

ParcelFileDescriptor.MODE_READ_ONLY));

throw new FileNotFoundException(uri.getPath());

}

@Override

public int delete(final Uri uri, final String selection, final String[] selectionArgs) {

throw new UnsupportedOperationException();

}

@Override

public Uri insert(final Uri uri, final ContentValues values) {

throw new UnsupportedOperationException();

}

@Override

public Cursor query(final Uri uri, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder) {

throw new UnsupportedOperationException();

}

@Override

public int update(final Uri uri, final ContentValues values, final String selection, final String[] selectionArgs) {

throw new UnsupportedOperationException();

}

}

And then, where I access my video files:

VideoView myVideoView = findViewById(R.id.videoView);

...

myVideoView.setVideoURI(

Uri.parse(

CachedActionProvider.CONTENT_URI_BASE + Uri.encode(videoFilename)));

...

myVideoView.start();

This is a really long-winded way of telling the VideoView to ask your ContentProvider for the file descriptor to the data. File descriptors aren't permissioned, so you open the file using your app's permissions and hand it off to the VideoView rather than asking the VideoView to open the file using its own permissions.

This fixes my issue and hopefully yours, too!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值