将媒体文件写入内部/外部存储 [英] Write media file to internal/external storage

查看:92
本文介绍了将媒体文件写入内部/外部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试找出一种将媒体文件写入内部/外部存储(主存储)的方法.要保存的文件的大小可以从几MB到50MB.我有适用于Droid X 2.3.3 Razr 2.3.5(我相信)的逻辑,但不适用于我的Galaxy Nexus(没有可移动存储,但内置v4.0.2的16Gig卡).我环顾四周,还没有找到与v4.0兼容的任何代码/示例.也许因为我没有实际的SD卡,所以这一切都错了吗?也许这是v4.0中的新功能?当前,当我在Galaxy Nexus上运行我的应用程序时,得到以下消息: System.err(19520):java.io.FileNotFoundException:

已更新

  InputStream inputStream = urlConnection.getInputStream();文件PATH = Environment.getExternalStorageDirectory();文件FILE =新文件(Environment.getExternalStorageDirectory()+"/" + FILENAME);ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();//buffer int bufferSize = 1024;int bufferLength = 0;byte [] buffer =新的byte [bufferSize];while(((bufferLength = inputStream.read(buffer))!= -1){byteBuffer.write(buffer,0,bufferLength);}byte [] temp = byteBuffer.toByteArray();FileOutputStream fos = new FileOutputStream(FILE);fos.write(temp);fos.close(); 

解决方案

您是否将此文件放在sdcard上的特定目录中?外部存储?

我认为您在清单中的权限是好的,因为未拒绝权限"不会增加,因此,如果将文件放在未创建的特定文件夹中,则应在文件上调用mkdirs()函数!/p>

I am currently trying to figure out a way to write a media file to internal/external storage (primary storage). The file to be saved could be any size from a few MBs to 50MBs. I have logic that works on my Droid X 2.3.3 Razr 2.3.5 (I believe) but does not work on my Galaxy Nexus (has no removable storage but a built in 16Gig card with v4.0.2). I have looked around and haven't found any code/samples that work with v4.0. Maybe I am approaching this all wrong since it doesn't have an actual sd card? maybe it is something new in v4.0? Currently when I run my application on the Galaxy Nexus I get this: System.err(19520): java.io.FileNotFoundException:

UPDATED

InputStream inputStream = urlConnection.getInputStream(); 
File PATH = Environment.getExternalStorageDirectory(); 
File FILE = new File(Environment.getExternalStorageDirectory()+ "/" + FILENAME); 
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); 
// buffer int bufferSize = 1024; int bufferLength = 0; byte[] buffer = new byte[bufferSize]; 
while ((bufferLength = inputStream.read(buffer)) != -1) { 
byteBuffer.write(buffer, 0, bufferLength); 
} 
byte[] temp = byteBuffer.toByteArray(); 
FileOutputStream fos = new FileOutputStream(FILE); 
fos.write(temp); 
fos.close();

解决方案

Are you putting this file in a specific directory on your sdcard?external storage?

I assume your permissions in your manifest are good because the 'permission denied is not raised' so maybe if you put the file in a specific folder, which is not created you should call the mkdirs() function on your file!

这篇关于将媒体文件写入内部/外部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆