我有输入流,我需要将其传递给视频视图:
但它说OutOfMemoryError
这是我的代码:
//to convert input stream to string
public static String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
return sb.toString();
}
//passing that string to video view
String total = convertStreamToString(inputStream);
Uri uri = Uri.parse(total);
videoHolder.setVideoURI(uri);
如何实现这一目标?
修改
这是堆栈跟踪
07-17 17:46:34.242: E/AndroidRuntime(5384): FATAL EXCEPTION: main
07-17 17:46:34.242: E/AndroidRuntime(5384): java.lang.OutOfMemoryError
07-17 17:46:34.242: E/AndroidRuntime(5384): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
07-17 17:46:34.242: E/AndroidRuntime(5384): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
07-17 17:46:34.242: E/AndroidRuntime(5384): at java.lang.StringBuilder.append(StringBuilder.java:216)
07-17 17:46:34.242: E/AndroidRuntime(5384): at com.abc.Utility.Utils.convertStreamToString(Utils.java:196)
07-17 17:46:34.242: E/AndroidRuntime(5384): at com.abc.xyz.VideoPlayer.onCreate(VideoPlayer.java:257)
07-17 17:46:34.242: E/AndroidRuntime(5384): at android.app.Activity.performCreate(Activity.java:4635)
07-17 17:46:34.242: E/AndroidRuntime(5384): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-17 17:46:34.242: E/AndroidRuntime(5384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2031)
07-17 17:46:34.242: E/AndroidRuntime(5384): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2092)
07-17 17:46:34.242: E/AndroidRuntime(5384): at android.app.ActivityThread.access$600(ActivityThread.java:126)
07-17 17:46:34.242: E/AndroidRuntime(5384): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1172)
07-17 17:46:34.242: E/AndroidRuntime(5384): at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 17:46:34.242: E/AndroidRuntime(5384): at android.os.Looper.loop(Looper.java:137)
07-17 17:46:34.242: E/AndroidRuntime(5384): at android.app.ActivityThread.main(ActivityThread.java:4586)
07-17 17:46:34.242: E/AndroidRuntime(5384): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 17:46:34.242: E/AndroidRuntime(5384): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 17:46:34.242: E/AndroidRuntime(5384): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-17 17:46:34.242: E/AndroidRuntime(5384): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-17 17:46:34.242: E/AndroidRuntime(5384): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:4)
好吧我自己已经解决了,但它在视频文件启动前有3到4秒的延迟
希望它有所帮助:
public static String getDataSource(String path) throws IOException {
if (!URLUtil.isNetworkUrl(path)) {
return path;
} else {
URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();
InputStream stream = cn.getInputStream();
if (stream == null)
throw new RuntimeException("stream is null");
File temp = File.createTempFile("mediaplayertmp", "dat");
temp.deleteOnExit();
String tempPath = temp.getAbsolutePath();
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[128];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
try {
stream.close();
out.close();
} catch (IOException ex) {
// Log.e(TAG, "error: " + ex.getMessage(), ex);
}
return tempPath;
}
}