手机内部文件存储——assets目录

一.创建

1./src/main/assets

2.切换到Project视图模式,在main下新建文件夹assets

二.特点

1.和res同等级别

2.主要是存放项目中的大文件

3.文件不受R类的管理

三.API

1.AssetManager  资产管理器

1-getAssets()   得到资产管理器

2-open(文件名)   返回文件的InputStream

2.ImageView:setImageBitmap(Bitmap  实例)  设置图片视图的位图

3.Bitmap   位图:BitmapFactory.decodeFile(图片文件路径)   使用工厂方法得到图片文件的Bitmap.

图片内部存储代码展示:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp3.MainActivity"
11     android:orientation="vertical">
12
13
14
15     <Button
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:text="保存资产文件到内部存储"
19         android:onClick="bt4_OnClick"/>
20
21     <ImageView
22         android:layout_width="wrap_content"
23         android:layout_height="30dp"
24         android:id="@+id/iv_1"
25         android:src="@drawable/f9"/>
26
27     <Button
28         android:layout_width="match_parent"
29         android:layout_height="wrap_content"
30         android:text="设置图片指向内部存储"
31         android:onClick="bt5_OnClick"/>
32
33
34
35
36
37 </LinearLayout>

图片手机内部存储

  1 package com.hanqi.testapp3;
  2
  3 import android.content.SharedPreferences;
  4 import android.content.res.AssetManager;
  5 import android.graphics.Bitmap;
  6 import android.graphics.BitmapFactory;
  7 import android.os.Environment;
  8 import android.support.v7.app.AppCompatActivity;
  9 import android.os.Bundle;
 10 import android.text.BidiFormatter;
 11 import android.view.View;
 12 import android.widget.EditText;
 13 import android.widget.ImageView;
 14 import android.widget.TextView;
 15 import android.widget.Toast;
 16
 17 import java.io.File;
 18 import java.io.FileInputStream;
 19 import java.io.FileOutputStream;
 20 import java.io.InputStream;
 21 import java.io.PrintStream;
 22
 23
 24 public class MainActivity extends AppCompatActivity {
 25
 26
 27     ImageView iv_1;
 28
 29     @Override
 30     protected void onCreate(Bundle savedInstanceState) {
 31         super.onCreate(savedInstanceState);
 32         setContentView(R.layout.activity_main);
 33
 34
 35         iv_1=(ImageView)findViewById(R.id.iv_1);
 36
 37     }
 38
 39
 40
 41     //保存资产文件到内部存储
 42     public  void bt4_OnClick(View v)
 43     {
 44         try {
 45
 46             //操作assets目录文件
 47
 48             //1.得到assetsManager
 49
 50             AssetManager am=getAssets();
 51
 52             //2.操作资产目录,边读边写入
 53             //1)读文件到内存  inputstream
 54             InputStream is=am.open("car.png");
 55
 56
 57             //2)写文件到目录  outputstream
 58             FileOutputStream fos=openFileOutput("test.png",MODE_PRIVATE);
 59
 60             //先读后写
 61             byte [] b=new byte[1024];
 62             int i=0;
 63
 64             while ((i=is.read(b))>0)
 65             {
 66                 fos.write(b,0,i);
 67             }
 68
 69             fos.close();
 70             is.close();
 71
 72             Toast.makeText(MainActivity.this, "保存文件成功", Toast.LENGTH_SHORT).show();
 73
 74
 75
 76         }
 77         catch (Exception e)
 78         {
 79             Toast.makeText(MainActivity.this, "保存文件出错", Toast.LENGTH_SHORT).show();
 80         }
 81     }
 82
 83     //设置图片指向内部存储
 84     public void bt5_OnClick(View v)
 85     {
 86
 87         //1.得到文件路径
 88         String path=getFilesDir().getAbsolutePath()+"/test.png";
 89
 90         Toast.makeText(MainActivity.this, "path="+path, Toast.LENGTH_LONG).show();
 91
 92         //2.从内部存储的图片得到  Bitmap,BitmapFactory.decodeFile("文件路径");
 93         Bitmap bm= BitmapFactory.decodeFile(path);
 94
 95         //3.设置图片视图的图片来源
 96         iv_1.setImageBitmap(bm);
 97     }
 98
 99
100 }

图片手机内部存储.java

时间: 2024-08-05 03:00:10

手机内部文件存储——assets目录的相关文章

Android——数据存储(课堂代码整理:SharedPreferences存储和手机内部文件存储)

layout文件: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="ma

数据存储——手机内部文件存储

一.特点 1.存储的是任意类型的文件 2.使用IO输入输出流操作文件 3.存放的目录:/data/data/包名/files/ 4.可以设置不被其他应用操作 5.应用卸载之后,数据同时被删除 二.API 1.FileOutputStream  文件输出流 1-openFileOutput(文件名,操作模式) mode  操作模式 1>MODE_PRIVATE,不能被别的应用访问,覆盖模式 2>MODE_APPEND,不能被别的应用访问,追加模式 2-close( )关闭输出流 2.PrintS

手机内部file存储

---恢复内容开始--- 将数据存储到文件中 package com.example.hzq_study; import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter; import android.app.Activity;import

手机外部文件存储(SD卡存储)

package com.atguigu.l04_datastorage; 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.app.Activity;

android-数据存储之手机内部file存储

一.基础概要 1.说明: 1>应用程序运行需要一些较大的数据或者图片可保存在手机内部 2>文件类型:任意 3>路径:/data/data/packageName/files/ 4>卸载应用时会删除此数据文件 5>也可以设置操作数据文件的权限(同SharedPreferences) 二.练习 1>FileInputStream fis=openFileInput("logo.png");     读取文件 2>FileOutputStream f

数据存储——手机外部文件存储

一.特点 1.把文件存储在手机外部存储空间(SD卡)里 2.存储的是任意类型的文件 3.使用IO输入输出流操作文件 4.文件路径 1-SD卡根目录/Android/data/包名/files/[ 文件类型],应用卸载后,数据同时被删除: 2-SD卡根目录/,应用卸载之后,数据不会被同时删除. 5.需要声明权限 1-android.permission.WRITE_EXTERNAL_STORAGE,写入文件: 2-MOUNT_UNMOUNT_FILESYSTEMS,创建和删除文件. 二.API 1

Androidproject文件下assets目录与res目录的差别

1. assets : 不会在R.java文件下生成对应的标记,assets目录能够自己创建目录,必须使用AssetsManager类进行訪问,存放到这里的资源在执行打包的时候都会打入程序安装包中, **2. res:会在R.java文件下生成标记,这里的资源会在执行打包操作的时候推断哪些被使用到了,没有被使用到的文件资源是不会打包到安装包中的. res/raw和assets目录来存放不须要系统编译成二进制的文件,比如字体文件等** **在res目录下还能够定义一下目录: res/anim :

【Android】14.1 内部文件存储和读取

分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 内部存储是指将应用程序建立的私有文件保存在内部存储器(移动经销商卖的那种容量较小的手机卡)中. 应用程序可通过OpenFileInput方法和OpenFileOutput方法读取内部存储设备上的这些文件. 1.OpenFileOutput方法 该方法打开应用程序私有文件,为写入设备做准备.默认情况下,写入的文件会覆盖同名的原文件.如果要打开的文件不存在,则创建一个新文件. 该方法的语法格式如下: public

手机内存文件存储练习

把用户的注册信息存储到文件里,登录成功后读出并显示出来,把代码发到博客上. 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4