• Assets


     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.Bundle;
     8 import android.os.Environment;
     9 import android.support.v7.app.AppCompatActivity;
    10 import android.view.View;
    11 import android.widget.EditText;
    12 import android.widget.ImageView;
    13 import android.widget.TextView;
    14 import android.widget.Toast;
    15 
    16 import java.io.File;
    17 import java.io.FileInputStream;
    18 import java.io.FileOutputStream;
    19 import java.io.InputStream;
    20 import java.io.PrintStream;
    21 
    22 public class MainActivity extends AppCompatActivity {
    23 
    24     ImageView iv_1;
    25 
    26     @Override
    27     protected void onCreate(Bundle savedInstanceState) {
    28         super.onCreate(savedInstanceState);
    29         setContentView(R.layout.activity_main);
    30 
    31         iv_1 = (ImageView)findViewById(R.id.iv_1);
    32 
    33     }
    34 
    35 //保存资产文件到内部存储
    36     public void bt4_OnClick(View v)
    37     {
    38         //操作assets目录的文件
    39 
    40         //1、得到assetsManager
    41         AssetManager am = getAssets();
    42 
    43         try {
    44             //2、操作资产目录,边读边写入
    45             //1)读文件到内存  inputstream
    46             InputStream is = am.open("qiyi.png");
    47 
    48             //2)写文件到目录  outputsteam
    49             FileOutputStream fos = openFileOutput("text.png",MODE_PRIVATE);
    50 
    51             //先读后写
    52             byte[] b = new byte[1024];
    53             int i = 0;
    54 
    55             while ((i=is.read(b)) > 0)
    56             {
    57                 fos.write(b,0,i);
    58             }
    59 
    60             fos.close();
    61             is.close();
    62 
    63             Toast.makeText(MainActivity.this, "保存文件成功", Toast.LENGTH_SHORT).show();
    64 
    65         }
    66         catch (Exception ex)
    67         {
    68             Toast.makeText(MainActivity.this, "保存文件出错", Toast.LENGTH_SHORT).show();
    69         }
    70 
    71     }
    72 
    73 
    74 
    75     //设置图片指向内部存储
    76     public void bt5_OnClick(View v)
    77     {
    78         //1、得到文件路径
    79         String path = getFilesDir().getAbsolutePath() + "/text.png";
    80 
    81         Toast.makeText(MainActivity.this, "path = " + path, Toast.LENGTH_SHORT).show();
    82 
    83         //2、从内部存储的图片得到 Bitmap,BitmapFactory.decodeFile("文件路径");
    84         Bitmap bm = BitmapFactory.decodeFile(path);
    85 
    86         //3、设置图片视图的图片来源
    87         iv_1.setImageBitmap(bm);
    88     }
    89 }
     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     <Button
    14         android:layout_width="match_parent"
    15         android:layout_height="wrap_content"
    16         android:text="保存资产文件到内部存储"
    17         android:onClick="bt4_OnClick"/>
    18 
    19     <ImageView
    20         android:layout_width="wrap_content"
    21         android:layout_height="wrap_content"
    22         android:id="@+id/iv_1"
    23         android:src="@drawable/apu"/>
    24 
    25     <Button
    26         android:layout_width="match_parent"
    27         android:layout_height="wrap_content"
    28         android:text="设置图片指向内部存储"
    29         android:onClick="bt5_OnClick"/>
    30 </LinearLayout>
  • 相关阅读:
    Object.assign()方法
    JavaScript对象(二)
    JavaScript对象(一)
    vue开发中遇到的问题集锦(2)
    Python:str.ljust()、str.rjust()、str.center()函数
    Python:如何将多个小字符串拼接成一个大的字符串
    Python:.join()函数
    Python:生成器表达式
    VS Code:快捷方式
    Python:如何调整字符串中文本的格式
  • 原文地址:https://www.cnblogs.com/future-zhenzhen/p/5536915.html
Copyright © 2020-2023  润新知