189 8069 5689

android的文件操作,android文件夹是干嘛的

安卓手机如何打开.pdf文件?

最佳经验

创新互联公司是一家专业从事成都做网站、网站制作的网络公司。作为专业网站设计公司,创新互联公司依托的技术实力、以及多年的网站运营经验,为您提供专业的成都网站建设、全网营销推广及网站设计开发服务!

本文由作者推荐

01

安卓手机打开.pdf文件:在百度手机助手或者在别的第三方下载工具里下载ireader,使用ireader软件打开。首先下载ireader软件安装在手机上,在手机文件管理器里找到pdf文件。打开该文件,这时候自动会默认使用ireader软件打开。这时候会提示需要安装pdf组件,点击确定之后会自动安装pdf组件。安装好之后,再次在文件管理器内打开pdf文件,这时候就可以阅读该文件了。

PDF是由Adobe Systems用于与应用程序、操作系统、硬件无关的方式进行文件交换所发展出的文件格式。PDF文件以PostScript语言图象模型为基础,无论在哪种打印机上都可保证精确的颜色和准确的打印效果,即PDF会忠实地再现原稿的每一个字符、颜色以及图象。

安卓手机打开.pdf 文件的方法:

1、首先在百度手机助手或者在别的第三方下载工具里下载ireader。

2、下载下来软件之后安装在手机上,在手机文件管理器里找到pdf文件。

3、打开该文件,这时候自动会默认使用ireader软件打开。这时候会提示需要安装pdf组件。

4、点击确定之后会自动安装pdf组件。安装好之后,再次在文件管理器内打开pdf文件。这时候就可以阅读该文件了。

安卓(Android)是一种基于Linux的自由及开放源代码的操作系统。主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。Android操作系统最初由Andy Rubin开发,主要支持手机。

Android 文件存储-将数据保存在data目录下

  在平常使用Android手机的时候,我们都知道,几乎每一个app都在/data/data/相应的包名的文件夹下保存数据。那这些数据怎么进行保存的呢?在这里,将简单的介绍一下。

  Context类中有一个openFileOutPut方法,这个方法可以将我们的数据保存在data目录下的文件里面。

  openFileOutput(String name, int mode)方法中带两个参数,第一个参数是文件名,这里只能写文件的名字,不能包含路径,因为所有的数据都保存在/data/data/应用包名/files/目录下;第二个参数是文件的操作模式,有MDOE_PRIVATE,MODE_APPEND,MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。

  其中MODE_PRIVATE模式的是默认的操作模式,每一次写入的内容时,都会覆盖前面的内容;MODE_APPEND模式表示的是每次写入的内容追加在前面的后面;MODE_WORLD_READABLE表示的是其他应用程序可以对该文件进行写的操作;MODE_WORLD_WRITEABLE表示的是其他应用程序可以对该文件进行读的操作。不过在后面的两种模式过于危险,google已经在Android 4.2中废弃了。

  openFileOutput()方法返回的是一个FileOutPutStream的对象,得到了这个对象,就可以使用Java的IO流来对文件的使用了。

  点击保存过后,就会把我们的数据保存在data目录下。

  如果我们想要查看的话,就可以在Android studio(我是2.3.2的版本)中找到Tools-Android-Android Device Monitor

  再打开/data/data/应用包名/files/,发现有一个文件,就是我们之前创建的一个文件。

  我们可以点击右上角的图标进行相应的导出工作,对相应的文件进行导出操作。

  在Context类中,与openFileOutput方法对应的是openFileInput方法,用户从data目录读取相应的数据。这个方法相较于openFileOutput方法简单一些。

效果示意图:

Android之怎么操作文件

步骤如下:

一、介绍:

此主要是介绍怎么把文件写入到手机存储上、怎么从手机存储上读取文件内容以及怎么把文件写到SDCard

二、新建一个android工程——FileOperate

目录如下:

三、清单列表AndroidManifest.xml的配置为:

?xml version="1.0" encoding="utf-8"?

manifest xmlns:android=""

package="com.example.files"

android:versionCode="1"

android:versionName="1.0"

uses-sdk android:minSdkVersion="8" /

application

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

!--单元测试 加这句--

uses-library android:name="android.test.runner" /

activity

android:name=".FileActivity"

android:label="@string/app_name"

intent-filter

action android:name="android.intent.action.MAIN" /

category android:name="android.intent.category.LAUNCHER" /

/intent-filter

/activity

/application

!-- 往SDCard的创建与删除文件权限 --

uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/

!-- 往SDCard写入数据权限 --

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/

!--单元测试加这句,其中android:targetPackage为当前应用的包名

android:targetPackage 目标包是指单元测试的类的上面包和manifest的

package="com.example.main" 保持一致

这样就决定了你建立测试类的时候也必须在这个包下面

--

instrumentation android:name="android.test.InstrumentationTestRunner"

android:targetPackage="com.example.files"

android:label="Test for my app"/

/manifest

四、FileActivity.java源码:

package com.example.files;

import java.io.FileNotFoundException;

import java.io.IOException;

import com.example.service.FileService;

import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class FileActivity extends Activity {

EditText fileName ;

EditText fileContent;

Button fileSave;

OnClickListener fileSaveListener;

OnClickListener fileSaveSDListener;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

fileSaveListener = new OnClickListener(){

@Override

public void onClick(View v) {

//setTitle("123");

//Toast.makeText(FileActivity.this, "123", Toast.LENGTH_LONG).show();

String fileNameStr = fileName.getText().toString();

String fileContentStr = fileContent.getText().toString();

FileService fileService = new FileService(getApplicationContext());

try {

fileService.save(fileNameStr,fileContentStr);

Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG).show();

} catch (FileNotFoundException e) {

Toast.makeText(getApplicationContext(), R.string.failure, Toast.LENGTH_LONG).show();

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

};

fileSaveSDListener = new OnClickListener(){

@Override

public void onClick(View v) {

//setTitle("123");

//Toast.makeText(FileActivity.this, "123", Toast.LENGTH_LONG).show();

String fileNameStr = fileName.getText().toString();

String fileContentStr = fileContent.getText().toString();

FileService fileService = new FileService(getApplicationContext());

try {

//判断SDCard是否存在并且可以读写

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){

fileService.saveToSD(fileNameStr,fileContentStr);

Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG).show();

}

else

{

Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG).show();

}

} catch (FileNotFoundException e) {

Toast.makeText(getApplicationContext(), R.string.sdcardfail, Toast.LENGTH_LONG).show();

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

};

Button fileSave = (Button) this.findViewById(R.id.fileSave);

fileSave.setOnClickListener(fileSaveListener);

Button fileSaveSD = (Button) this.findViewById(R.id.fileSaveSD);

fileSaveSD.setOnClickListener(fileSaveSDListener);

fileName = (EditText) this.findViewById(R.id.fileName);

fileContent = (EditText) this.findViewById(R.id.fileContent);

}

}

五、FileService.java源码:

package com.example.service;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import android.content.Context;

import android.os.Environment;

public class FileService {

private Context context;

public FileService(Context context) {

super();

this.context = context;

}

/**

* 写入文件到SD卡

* @throws IOException

*/

public void saveToSD(String fileNameStr, String fileContentStr) throws IOException{

//备注:Java File类能够创建文件或者文件夹,但是不能两个一起创建

File file = new File("/mnt/sdcard/myfiles");

// if(!file.exists())

// {

// file.mkdirs();

// }

// File sd=Environment.getExternalStorageDirectory();

// String path=sd.getPath()+"/myfiles";

// File file=new File(path);

// if(file.exists()){

// file.delete();

// }

if(!file.exists()){

file.mkdir();

}

File file1 = new File(file, fileNameStr);

FileOutputStream fos = new FileOutputStream(file1);

fos.write(fileContentStr.getBytes());

fos.close();

}

/**

* 保存文件到手机

* @param fileNameStr 文件名

* @param fileContentStr 文件内容

* @throws IOException

*/

public void save(String fileNameStr, String fileContentStr) throws IOException {

//私有操作模式:创建出来的文件只能被本应用访问,其它应用无法访问该文件,另外采用私有操作模式创建的文件,写入文件中的内容会覆盖原文件的内容

FileOutputStream fos = context.openFileOutput(fileNameStr, context.MODE_PRIVATE);

fos.write(fileContentStr.getBytes());

fos.close();

}

public void saveAppend(String fileNameStr, String fileContentStr) throws IOException {

//追加操作模式:不覆盖源文件,但是同样其它应用无法访问该文件

FileOutputStream fos = context.openFileOutput(fileNameStr, context.MODE_APPEND);

fos.write(fileContentStr.getBytes());

fos.close();

}

public void saveReadable(String fileNameStr, String fileContentStr) throws IOException {

//读取操作模式:可以被其它应用读取,但不能写入

FileOutputStream fos = context.openFileOutput(fileNameStr, context.MODE_WORLD_READABLE);

fos.write(fileContentStr.getBytes());

fos.close();

}

public void saveWriteable(String fileNameStr, String fileContentStr) throws IOException {

//写入操作模式:可以被其它应用写入,但不能读取

FileOutputStream fos = context.openFileOutput(fileNameStr, context.MODE_WORLD_WRITEABLE);

fos.write(fileContentStr.getBytes());

fos.close();

}

public void saveReadWriteable(String fileNameStr, String fileContentStr) throws IOException {

//读写操作模式:可以被其它应用读写

FileOutputStream fos = context.openFileOutput(fileNameStr,

context.MODE_WORLD_READABLE+context.MODE_WORLD_WRITEABLE);

fos.write(fileContentStr.getBytes());

fos.close();

}

/**

* 读取文件内容

* @param fileNamestr 文件名

* @return

* @throws IOException

*/

public String read(String fileNamestr) throws IOException

{

FileInputStream fis = context.openFileInput(fileNamestr);

ByteArrayOutputStream bos = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int len = 0;

while((len = fis.read(buffer)) != -1){

bos.write(buffer,0,len);

}

byte[] data = bos.toByteArray();

return new String(data);

}

}

六、FileServiceTest.java源码:

package com.junit.test;

import com.example.service.FileService;

import android.test.AndroidTestCase;

import android.util.Log;

public class FileServiceTest extends AndroidTestCase {

/**

* 测试私有模式的读取

* @throws Exception

*/

public void testRead() throws Exception{

FileService fileService = new FileService(this.getContext());

String fileContext = fileService.read("fileSave.txt");

Log.i("FileRead", fileContext);

}

/**

* 测试追加模式的写入

* @throws Exception

*/

public void testSaveAppend() throws Exception{

FileService fileService = new FileService(this.getContext());

fileService.saveAppend("fileAppendSave.txt", "你好");

}

/**

* 测试读取模式的读取

* @throws Exception

*/

public void testSaveReadable() throws Exception{

FileService fileService = new FileService(this.getContext());

fileService.saveReadable("fileSaveReadable.txt", "wonengbeiduquma");

}

/**

* 测试写入模式的写入

* @throws Exception

*/

public void testSaveWriteable() throws Exception{

FileService fileService = new FileService(this.getContext());

fileService.saveWriteable("fileSaveWriteable.txt", "wonengbeiduquma");

}

/**

* 测试读写模式的写入

* @throws Exception

*/

public void testSaveReadWriteable() throws Exception{

FileService fileService = new FileService(this.getContext());

fileService.saveReadWriteable("fileSaveReadWriteable.txt", "我能被读写吗?");

}

}

更多信息,请阅读此篇文章:

Android 中的文件读写操作

IO流(操作文件内容): 字节流

参考:

AssetManager

assets 文件夹用于存储应用需要的文件,在安装后可直接从其中读取使用或者写入本地存储中

Android Studio 默认不建立该文件夹,可以手动新建 : app - src - main - assets

或者,右键 main - New - Folder - Assets Folder

AssetManager 对象可以直接访问该文件夹:

获取方法:

使用函数 open 可以打开 assets 文件夹中对象,返回一个 InputStream 对象:

open

获取方法:


分享标题:android的文件操作,android文件夹是干嘛的
URL网址:http://cdxtjz.cn/article/dsejcgj.html

其他资讯