Android学习之文件File

来源:互联网 发布:原生js创建节点对象 编辑:程序博客网 时间:2024/06/09 15:13

       Android中文件file的使用与其他平台类似的基于磁盘文件系统(disk-based file systems)。File 对象非常适合用来读写那种流式顺序的数据,例如:适合用来读写图片文件或者是网络中交换的数据。
       文件存储分为内部存储(Internal Storage)和外部存储(External Storage)两种。这两个名称来自于早先的Android系统中,那时候大多数的设备都内置了不可变的内存(internal storage),再加上一个类似SD card(external storage)可以卸载的存储部件。后来有一些设备把"internal" 与 "external" 的部分都做成不可卸载的内置存储了,虽然如此,但是这一整块还是从逻辑上还是被划分为"internal"与"external"的。

      Internal storage:总是可用的;这里的文件默认是只能被你的app所访问的;当用户卸载你的app的时候,系统会把internal里面的相关文件都清除干净;Internal是在你想确保不被用户与其他app所访问的最佳存储区域。

     External storage:并不总是可用的,因为用户可以选择把这部分作为USB存储模式,这样就不可以访问了;是大家都可以访问的,因此保存到这里的文件是失去访问控制权限的;当用户卸载你的app时,系统仅仅会删除external根目录(getExternalFilesDir())下的相关文件。

     External是在你不需要严格的访问权限并且你希望这些文件能够被其他app所共享或者是允许用户通过电脑访问时的最佳存储区域。
     Tip: 尽管app是默认被安装到internal storage的,你还是可以通过在程序的manifest文件中声明android:installLocation 属性来指定程序也可以被安装到external storage。当某个程序的安装文件很大,这个程序能够提供安装到external storage的选项。

     获取External存储的权限:为了写数据到external storage, 你必须在你的manifest文件中请求WRITE_EXTERNAL_STORAGE权限:

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
</manifest>

       Caution:目前,所有的apps都可以在不指定某个专门的权限下做读external storage的动作。但是,这在以后的版本中会有所改变。如果你的app只需要读的权限(不是写), 那么你将需要声明 READ_EXTERNAL_STORAGE 权限。为了确保你的app能持续地正常工作,你需要现在就声明读权限。

<manifest ...>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    ...
</manifest>

        但是,如果你的程序有声明读的权限,那么就默认有了写的权限。对于internal storage,你不需要声明任何权限,因为你的程序默认就有读写程序目录下的文件的权限。


       以下图所示为例:

 

    其xml布局如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ImageView        android:id="@+id/file_imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/xqx1" />    <TextView        android:id="@+id/file_dir_txt"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <Button        android:id="@+id/file_write_btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="写入图片到区域" />    <Button        android:id="@+id/file_pic_reader_btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="读取图片到区域" />    <ImageView        android:id="@+id/file_show_imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>


       其代码如下:

package com.sc.android.file;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import com.sc.android.R;public class MyFileActivity extends Activity {private TextView mDirtxt;private Button mWriteBtn, mReaderBtn;private ImageView mShowImg, mWriteImg;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_file_layout);mDirtxt = (TextView) findViewById(R.id.file_dir_txt);mWriteBtn = (Button) findViewById(R.id.file_write_btn);mReaderBtn = (Button) findViewById(R.id.file_pic_reader_btn);mShowImg = (ImageView) findViewById(R.id.file_show_imageView1);mWriteImg = (ImageView) findViewById(R.id.file_imageView1);Drawable drawable = getResources().getDrawable(R.drawable.xqx1);mWriteImg.setImageDrawable(drawable);final String fileEdress = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath();mDirtxt.setText(fileEdress);mWriteBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (!isExternalStorageWritable()) {Toast.makeText(MyFileActivity.this, "外部存储不可用",Toast.LENGTH_SHORT).show();return;}File file = new File(fileEdress, "mypic.png");FileOutputStream out;try {out = new FileOutputStream(file);Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xqx1);bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);out.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}});mReaderBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (!isExternalStorageWritable()) {Toast.makeText(MyFileActivity.this, "外部存储不可用",Toast.LENGTH_SHORT).show();return;}File file = new File(fileEdress, "mypic.png");mDirtxt.setText(file.getPath());Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());mShowImg.setImageBitmap(bitmap);}});}/** * 检查外部存储是否可用 */private boolean isExternalStorageWritable() {String state = Environment.getExternalStorageState();if (state.equals(Environment.MEDIA_MOUNTED)) {return true;} else {return false;}}}


 

     

0 0
原创粉丝点击