android 获取本地文件怎么获取手机本地视频

Android 获取手机本地IP - 博客频道 - CSDN.NET
分类:Android开发
private String getLocalIPAddress() {
&&&&& try {
&&&&&&&&&& for (Enumeration&NetworkInterface& en = NetworkInterface.getNetworkInterface(); en.hasMoreElements(); ) {
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& NetworkInterface intf = en.nextElement();
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& for (Enumeration&InetAddress& enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& InetAddress inetAddress = enumIpAddr.nextElements();
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& if ( !inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& // 这里增加限定条件inetAddress instanceof Inet4Address主要是在Android4.0高版本中可能优先得到的是IPV6地址
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& return inetAddress.getHostAddress().toString();
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&& }
&&&&& } catch (SocketException& ex) {
&&&&&&&&&&&&& Log.e(&getIP&, ex.toString());
&&&&&&&&&&&&& ex.printStackTrace();
&&&&&&&&&&&&&&
排名:千里之外
(41)(12)(4)(1)(1)(1)(20)(1)(3)(2)(3)Android之本地数据存储(二):File - 推酷
Android之本地数据存储(二):File
一. 创建File对象
创建File对象可使用new File()方法实现。具体参数如下:
File(File dir, String name)
File对象类型的目录路径,name为文件名或目录名。
File(String path)
path为新File对象的路径。
File(String dirPath, String name)
dirPath为指定的文件路径,name为文件名或目录名。
File(URI uri)
使用URI指定路径来创建新的File对象。
二. File对象的方法
1. File对象判断方法
boolean canExecute()
判断文件或目录是否可执行。
boolean canRead()
判断文件或目录是否可读。
boolean canWrite()
判断文件或目录是否可写。
boolean equals(Object obj)
判断obj和调用的对象是否相同。
boolean exists()
判断文件或目录是否存在。
boolean isAbsolute()
判断当前文件路径是否为绝对路径。
boolean isDirectory()
判断File对象是否是文件夹。
boolean isFile()
判断File对象是否是文件。
boolean isHidden()
判断是否为操作系统定义的隐藏文件。
2. File对象属性返回方法
File getAbsoluteFile()
返回一个新的文件,该文件的绝对路径是调用的File的路径。
String getAbsolutePath()
返回该文件的绝对路径。
long getFreeSpace()
返回所在分区上剩余的字节数量,包括当前File的路径。
String getName()
获得文件名或文件名。
String getParent()
获得文件或文件夹的父目录名。
String getPath()
获取相对路径。
long getTotalSpace()
返回分区的总字节大小。
long getUsableSpace()
返回分区可用字节的大小。
long lastModified()
返回最后一次修改该文件的时间,以毫秒计算,从日开始算。
long length()
返回文件的长度,单位为字节。
String toString()
返回一个对象的字符串表示。
URI toURI()
返回一个文件的URI。
3. File对象属性设置方法
boolean renameTo(File newPath)
修改文件夹名和文件名。
boolean setLastModified(long time)
设置最后一次修改该文件的时间,以毫秒计算,从日开始算。
boolean setReadOnly()
设置文件只有读权限。
boolean setReadable(boolean readable[, boolean ownerOnly])
设置文件的读状态。
boolean setWritable([boolean writable,] boolean ownerOnly])
设置文件的写状态。
4. File对象的增删查改
boolean createNewFile()
创建文件或文件夹。
boolean delete()
删除文件夹或文件。
File[] listFiles()
列出文件夹下的所有文件和文件夹名。
boolean mkdir()
创建一个文件夹。当父目录存在才能成功创建。
boolean mkdirs()
创建一个文件夹。当父目录不存在时,则创建父目录。
三. I/O文件流访问文件
Java提供了一套完整的I/O流体系,包括FileInputStream、FileOutputStream等,通过这些I/O流可以非常方便地访问磁盘上的文件内容。
Context提供了如下两个方法来打开本应用程序的数据文件的文件I/O流:
FileInputStream openFileInput(String name) :打开应用程序的数据文件下的name文件对应的输入流;
FileOutputStream openFileOutput(String name, int mode) :打开应用程序的数据文件下的name文件对应的输出流。
其中,openFileOutput(String name, int mode)中的mode参数表示打开文件的模式,可取的值如下:
Context.MODE_PRIVATE :该文件只能被本应用程序访问。如果写入,则覆盖原文件;
Context.MODE_APPEND :该文件以追加方式打开文件,只能被本应用程序访问;
Context.MODE_WORLD_READABLE :该文件的内容可以被其他程序读取;
Context.MODE__WORLD_WRITEABLE :该文件的内容可以被其他程序读写。如果写入,则覆盖原文件。
注意,如果希望文件模式叠加,则可以使用加号连接,如 & Context.MODE_APPEND + Context.MODE_WORLD_WRITEABLE &来 表示其他应用以追加方式读写文件。
此外,Context还提供了如下几个方法来访问应用程序的数据文件夹:
File getFilesDir()
返回本应用程序的数据文件夹的绝对路径。在这里获取到的是&/data/data/ &包名& /files/&目录,返回File对象。
File getCacheDir()
返回本应用程序默认的缓存文件存放路径。用于获取&/data/data/ &包名& /cache/&目录,返回File对象。
String[] fileList()
返回本应用程序的数据文件夹的全部文件。
deleteFile(String)
删除本应用程序的数据文件夹下的指定文件。
四. File存储实例
1 . 写入并保存文件内容:
// 写入并保存文件内容
public void writeFiles(String content, String fileName) {
// 初始化文件输出流
FileOutputStream fileOutputStream =
// 以追加模式打开文件输出流
fileOutputStream = openFileOutput(fileName, MODE_APPEND);
fileOutputStream.write(content.getBytes());
// 关闭文件输出流
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
2 . 读取文件内容:
// 读取文件内容
public String readFiles(String fileName) {
// 定义文件内容字符串
String content =
// 文件输入流
FileInputStream fileInputStream =
// 打开文件输入流
fileInputStream = openFileInput(fileName);
// 将文件输入流存放在ByteArrayOutputStream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 定义每次读取一个字节
byte[] buffer = new byte[1024];
// 定义每次读取的字节长度
int len = 0;
// 读取文件输入流的内容,并存入ByteArrayOutputStream中
while ((len = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
// 将文件输入流数据以字符串的形式存放
content = outputStream.toString();
// 关闭文件输入流
fileInputStream.close();
// 关闭ByteArrayOutputStream
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
// 返回文件内容
3 . 页面展示逻辑:
private EditText dataI
private Button dataShowB
private TextView dataShowT
private String fileName = &test.txt&;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataInput = (EditText) findViewById(R.id.data_input);
dataShowBtn = (Button) findViewById(R.id.data_show_btn);
dataShowText = (TextView) findViewById(R.id.data_show_text);
// 按钮点击,则将EditText中的内容写入文件中,并展示出来
dataShowBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 定义存放写入的文件名
// 将EditorText中的输入内容以追加模式写入文件中
writeFiles(dataInput.getText().toString(), fileName);
// 输出文件内容,并展示
dataShowText.setText(readFiles(fileName));
4 . activity_main.xml布局文件如下:
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout
xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
tools:showIn=&@layout/activity_main&
android:orientation=&vertical&&
android:id=&@+id/data_input&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:lines=&4&
android:text=&&/&
android:id=&@+id/data_show_btn&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&Write In And Read&/&
android:id=&@+id/data_show_text&
android:layout_width=&match_parent&
android:layout_height=&wrap_content& /&
&/LinearLayout&
5 . 可在DDMS中查看到新建的text文件,文件路径为:& /data/data/ &包名& /files/test.txt &。
6 . 实例效果图如下:
五. 读写SD卡上的文件
当程序通过Context的openFileInputStream、OpenFileOutputStream来打开文件输入流、输出流时,程序所打开的都是应用程序的数据文件夹里的文件。但这样存储的文件大小比较有限,因为手机内置的存储空间不大。而SD卡可大大地扩充收的存储能力。
为了更好地存取应用程序的大文件数据,应用程序需要读写SD卡上的文件。
读、写SD卡上的文件的步骤如下:
第1步:在AndroidMainfest.xml文件中添加读写SD的权限
&!-- 在SD卡中创建与删除文件的权限 --&
&uses-permission android:name=&android.permission.MOUNT_UNMOUNT_FILESYSTEMS& /&
&!-- 向SD卡中写入数据的权限 --&
&uses-permission android:name=&android.permission.WRITE_EXTERNAL_STORAGE& /&
第2步:判断手机上是否插入SD卡,并且应用程序具有读写SD卡的功能
// 当手机上插入SD卡,并且应用程序具有读写SD卡的功能
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
第3步:调用getExternalStorageDirectory()来获得SD的根目录
File SDCardRoot = Environment.getExternalStorageDirectory();
第4步:读写SD卡中的文件
使用FileInputStream、FileOutputStream、FileReader或FileWrite,来读写SD卡中的文件。这一步与在手机内存中操作文件的方法一样。
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致Android本地视频播放器开发 - 搜索本地视频(1)_Linux编程_Linux公社-Linux系统门户网站
你好,游客
Android本地视频播放器开发
搜索本地视频(1)
来源:Linux社区&
作者:jwzhangjie
这一章的主要内容是搜索手机本地视频,添加到ListView列表里,每一个表项含有这个视频的缩略图,视频的播放时间,视频的标题,在搜索本地视频(1)中我们先制作搜索功能。&Video.java--视频相关的属性类
package com.zhangjie.graduation.videopalyer.
import java.io.S
import com.zhangjie.ponent.LoadedI
public class Video implements Serializable{& & /**& * & */&private static final long serialVersionUID = -0367956L;&& & private S& & private S& & private S& & private String displayN& & private String mimeT& & private S& && & p& & private LoadedI
& & /**& & * & & */& & public Video() {& & & & super();& & }
& & /**& & * @param id& & * @param title& & * @param album& & * @param artist& & * @param displayName& & * @param mimeType& & * @param data& & * @param size& & * @param duration& & */& & public Video(int id, String title, String album, String artist,& & & & & & String displayName, String mimeType, String path, long size,& & & & & & long duration) {& & & & super();& & & & this.id =& & & & this.title =& & & & this.album =& & & & this.artist =& & & & this.displayName = displayN& & & & this.mimeType = mimeT& & & & this.path =& & & & this.size =& & & & this.duration =& & }
& & public int getId() {& & & && & }
& & public void setId(int id) {& & & & this.id =& & }
& & public String getTitle() {& & & && & }
& & public void setTitle(String title) {& & & & this.title =& & }
& & public String getAlbum() {& & & && & }
& & public void setAlbum(String album) {& & & & this.album =& & }
& & public String getArtist() {& & & && & }
& & public void setArtist(String artist) {& & & & this.artist =& & }
& & public String getDisplayName() {& & & & return displayN& & }
& & public void setDisplayName(String displayName) {& & & & this.displayName = displayN& & }
& & public String getMimeType() {& & & & return mimeT& & }
& & public void setMimeType(String mimeType) {& & & & this.mimeType = mimeT& & }
& & public String getPath() {& & & && & }
& & public void setPath(String path) {& & & & this.path =& & }
& & public long getSize() {& & & && & }
& & public void setSize(long size) {& & & & this.size =& & }
& & public long getDuration() {& & & && & }
& & public void setDuration(long duration) {& & & & this.duration =& & }& & & & public LoadedImage getImage(){& & &&pre class="java" name="code"&& & & }& & & & public void setImage(LoadedImage image){& & &this.image =& & }
AbstructProvider.java ---一个接口,来获取搜索的视频的一个集合package com.zhangjie.graduation.videopalyer.
import java.util.L
public interface AbstructProvider {&& & public List&Video& getList();& & }
VideoProvider.java--- 实现AbstructProvider接口,通过cursor来搜索视频的相关信息package com.zhangjie.graduation.videopalyer.
import java.util.ArrayLimport java.util.L
import .content.Cimport android.database.Cimport android.provider.MediaS
public class VideoProvider implements AbstructProvider {& & private C& & & & public VideoProvider(Context context) {& & & & this.context =& & }& & & & @Override& & public List&Video& getList() {& & & & List&Video& list =& & & & if (context != null) {& & & & & & Cursor cursor = context.getContentResolver().query(& & & & & & & & & & MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null,& & & & & & & & & & null, null);& & & & & & if (cursor != null) {& & & & & & & & list = new ArrayList&Video&();& & & & & & & & while (cursor.moveToNext()) {& & & & & & & & & & int id = cursor.getInt(cursor& & & & & & & & & & & & & & .getColumnIndexOrThrow(MediaStore.Video.Media._ID));& & & & & & & & & & String title = cursor& & & & & & & & & & & & & & .getString(cursor& & & & & & & & & & & & & & & & & & .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));& & & & & & & & & & String album = cursor& & & & & & & & & & & & & & .getString(cursor& & & & & & & & & & & & & & & & & & .getColumnIndexOrThrow(MediaStore.Video.Media.ALBUM));& & & & & & & & & & String artist = cursor& & & & & & & & & & & & & & .getString(cursor& & & & & & & & & & & & & & & & & & .getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST));& & & & & & & & & & String displayName = cursor& & & & & & & & & & & & & & .getString(cursor& & & & & & & & & & & & & & & & & & .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME));& & & & & & & & & & String mimeType = cursor& & & & & & & & & & & & & & .getString(cursor& & & & & & & & & & & & & & & & & & .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));& & & & & & & & & & String path = cursor& & & & & & & & & & & & & & .getString(cursor& & & & & & & & & & & & & & & & & & .getColumnIndexOrThrow(MediaStore.Video.Media.DATA));& & & & & & & & & & long duration = cursor& & & & & & & & & & & & & & .getInt(cursor& & & & & & & & & & & & & & & & & & .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));& & & & & & & & & & long size = cursor& & & & & & & & & & & & & & .getLong(cursor& & & & & & & & & & & & & & & & & & .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));& & & & & & & & & & Video video = new Video(id, title, album, artist, displayName, mimeType, path, size, duration);& & & & & & & & & & list.add(video);& & & & & & & & }& & & & & & & & cursor.close();& & & & & & }& & & & }& & & && & }
最后在主类中使用如下代码来获取最终得到的视频相关信息集合
AbstructProvider provider = new VideoProvider(this);List&Video&& listVideos = provider.getList();
在上面的listVideos包含了本地所有的视频的相关信息,下一章将会用到listVideos数据。
【内容导航】
相关资讯 & & &
& (03/02/:51)
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款
小火种 发表于 小白表示好难}

我要回帖

更多关于 android 获取本地时间 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信