最佳經(jīng)驗

創(chuàng)新互聯(lián)公司是一家專業(yè)從事成都做網(wǎng)站、網(wǎng)站制作的網(wǎng)絡(luò)公司。作為專業(yè)網(wǎng)站設(shè)計公司,創(chuàng)新互聯(lián)公司依托的技術(shù)實力、以及多年的網(wǎng)站運營經(jīng)驗,為您提供專業(yè)的成都網(wǎng)站建設(shè)、全網(wǎng)營銷推廣及網(wǎng)站設(shè)計開發(fā)服務(wù)!
本文由作者推薦
01
安卓手機打開.pdf文件:在百度手機助手或者在別的第三方下載工具里下載ireader,使用ireader軟件打開。首先下載ireader軟件安裝在手機上,在手機文件管理器里找到pdf文件。打開該文件,這時候自動會默認使用ireader軟件打開。這時候會提示需要安裝pdf組件,點擊確定之后會自動安裝pdf組件。安裝好之后,再次在文件管理器內(nèi)打開pdf文件,這時候就可以閱讀該文件了。
PDF是由Adobe Systems用于與應(yīng)用程序、操作系統(tǒng)、硬件無關(guān)的方式進行文件交換所發(fā)展出的文件格式。PDF文件以PostScript語言圖象模型為基礎(chǔ),無論在哪種打印機上都可保證精確的顏色和準確的打印效果,即PDF會忠實地再現(xiàn)原稿的每一個字符、顏色以及圖象。
安卓手機打開.pdf 文件的方法:
1、首先在百度手機助手或者在別的第三方下載工具里下載ireader。
2、下載下來軟件之后安裝在手機上,在手機文件管理器里找到pdf文件。
3、打開該文件,這時候自動會默認使用ireader軟件打開。這時候會提示需要安裝pdf組件。
4、點擊確定之后會自動安裝pdf組件。安裝好之后,再次在文件管理器內(nèi)打開pdf文件。這時候就可以閱讀該文件了。
安卓(Android)是一種基于Linux的自由及開放源代碼的操作系統(tǒng)。主要使用于移動設(shè)備,如智能手機和平板電腦,由Google公司和開放手機聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。Android操作系統(tǒng)最初由Andy Rubin開發(fā),主要支持手機。
??在平常使用Android手機的時候,我們都知道,幾乎每一個app都在/data/data/相應(yīng)的包名的文件夾下保存數(shù)據(jù)。那這些數(shù)據(jù)怎么進行保存的呢?在這里,將簡單的介紹一下。
??Context類中有一個openFileOutPut方法,這個方法可以將我們的數(shù)據(jù)保存在data目錄下的文件里面。
??openFileOutput(String name, int mode)方法中帶兩個參數(shù),第一個參數(shù)是文件名,這里只能寫文件的名字,不能包含路徑,因為所有的數(shù)據(jù)都保存在/data/data/應(yīng)用包名/files/目錄下;第二個參數(shù)是文件的操作模式,有MDOE_PRIVATE,MODE_APPEND,MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。
??其中MODE_PRIVATE模式的是默認的操作模式,每一次寫入的內(nèi)容時,都會覆蓋前面的內(nèi)容;MODE_APPEND模式表示的是每次寫入的內(nèi)容追加在前面的后面;MODE_WORLD_READABLE表示的是其他應(yīng)用程序可以對該文件進行寫的操作;MODE_WORLD_WRITEABLE表示的是其他應(yīng)用程序可以對該文件進行讀的操作。不過在后面的兩種模式過于危險,google已經(jīng)在Android 4.2中廢棄了。
??openFileOutput()方法返回的是一個FileOutPutStream的對象,得到了這個對象,就可以使用Java的IO流來對文件的使用了。
??點擊保存過后,就會把我們的數(shù)據(jù)保存在data目錄下。
??如果我們想要查看的話,就可以在Android studio(我是2.3.2的版本)中找到Tools-Android-Android Device Monitor
??再打開/data/data/應(yīng)用包名/files/,發(fā)現(xiàn)有一個文件,就是我們之前創(chuàng)建的一個文件。
??我們可以點擊右上角的圖標進行相應(yīng)的導(dǎo)出工作,對相應(yīng)的文件進行導(dǎo)出操作。
??在Context類中,與openFileOutput方法對應(yīng)的是openFileInput方法,用戶從data目錄讀取相應(yīng)的數(shù)據(jù)。這個方法相較于openFileOutput方法簡單一些。
效果示意圖:
步驟如下:
一、介紹:
此主要是介紹怎么把文件寫入到手機存儲上、怎么從手機存儲上讀取文件內(nèi)容以及怎么把文件寫到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的創(chuàng)建與刪除文件權(quán)限 --
uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/
!-- 往SDCard寫入數(shù)據(jù)權(quán)限 --
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/
!--單元測試加這句,其中android:targetPackage為當前應(yīng)用的包名
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類能夠創(chuàng)建文件或者文件夾,但是不能兩個一起創(chuàng)建
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 文件內(nèi)容
* @throws IOException
*/
public void save(String fileNameStr, String fileContentStr) throws IOException {
//私有操作模式:創(chuàng)建出來的文件只能被本應(yīng)用訪問,其它應(yīng)用無法訪問該文件,另外采用私有操作模式創(chuàng)建的文件,寫入文件中的內(nèi)容會覆蓋原文件的內(nèi)容
FileOutputStream fos = context.openFileOutput(fileNameStr, context.MODE_PRIVATE);
fos.write(fileContentStr.getBytes());
fos.close();
}
public void saveAppend(String fileNameStr, String fileContentStr) throws IOException {
//追加操作模式:不覆蓋源文件,但是同樣其它應(yīng)用無法訪問該文件
FileOutputStream fos = context.openFileOutput(fileNameStr, context.MODE_APPEND);
fos.write(fileContentStr.getBytes());
fos.close();
}
public void saveReadable(String fileNameStr, String fileContentStr) throws IOException {
//讀取操作模式:可以被其它應(yīng)用讀取,但不能寫入
FileOutputStream fos = context.openFileOutput(fileNameStr, context.MODE_WORLD_READABLE);
fos.write(fileContentStr.getBytes());
fos.close();
}
public void saveWriteable(String fileNameStr, String fileContentStr) throws IOException {
//寫入操作模式:可以被其它應(yīng)用寫入,但不能讀取
FileOutputStream fos = context.openFileOutput(fileNameStr, context.MODE_WORLD_WRITEABLE);
fos.write(fileContentStr.getBytes());
fos.close();
}
public void saveReadWriteable(String fileNameStr, String fileContentStr) throws IOException {
//讀寫操作模式:可以被其它應(yīng)用讀寫
FileOutputStream fos = context.openFileOutput(fileNameStr,
context.MODE_WORLD_READABLE+context.MODE_WORLD_WRITEABLE);
fos.write(fileContentStr.getBytes());
fos.close();
}
/**
* 讀取文件內(nèi)容
* @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", "我能被讀寫嗎?");
}
}
更多信息,請閱讀此篇文章:
IO流(操作文件內(nèi)容): 字節(jié)流
參考:
AssetManager
assets 文件夾用于存儲應(yīng)用需要的文件,在安裝后可直接從其中讀取使用或者寫入本地存儲中
Android Studio 默認不建立該文件夾,可以手動新建 : app - src - main - assets
或者,右鍵 main - New - Folder - Assets Folder
AssetManager 對象可以直接訪問該文件夾:
獲取方法:
使用函數(shù) open 可以打開 assets 文件夾中對象,返回一個 InputStream 對象:
open
獲取方法:
網(wǎng)頁名稱:android的文件操作,android文件夾是干嘛的
轉(zhuǎn)載來于:http://chinadenli.net/article17/dsejcgj.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、軟件開發(fā)、網(wǎng)站制作、服務(wù)器托管、網(wǎng)站內(nèi)鏈、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)