zoukankan      html  css  js  c++  java
  • android 41 Environment

    assets通常存储音频视频文件,但不要太大。
    Environment可以获取sd卡的相关信息,
    sd卡的根路径:/storage/sdcard

     

    activity.java

    package com.sxt.day06_07;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Environment;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        private static final String FILE_NAME = "sxt_logo.png";
    //将工程assets下的sxt_logo.png图片保存进sd卡
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_UNMOUNTED)){//MEDIA_UNMOUNTED没有挂载
                //若没有sd卡
                Toast.makeText(this, "sd卡没有准备好", 2000).show();
                return ;
            }
            InputStream in = null;
            OutputStream out=null;
            try {
                in=getAssets().open(FILE_NAME);//getAssets返回AssetManager对象(管理Asset的管理器),获取了工程磁盘assets下的sxt_logo.png图片
                File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);//获取sd卡的保存图pain的路径/storage/sdcard/Pictures
                File file=new File(dir, FILE_NAME);
                out=new FileOutputStream(file);
                int len;
                byte[] buffer=new byte[8*1024];//每次读取8K
                while((len=in.read(buffer))!=-1){
                    out.write(buffer, 0, len);//将内存中的buffer写到输出流代表的sd卡
                }
                Toast.makeText(MainActivity.this, "文件保存完毕", 2000).show();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    if(out!=null){
                        out.close();
                    }
                    if(in!=null){
                        in.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }

    工程描述文件:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.sxt.day06_07"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="18" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>   申请读sd卡的权限
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>         申请写sd卡的权限
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.sxt.day06_07.MainActivity"
                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>
    
    </manifest>
  • 相关阅读:
    数学图形(1.12) 螺线
    数学图形(1.11) 玫瑰线
    数学图形(1.10) 双曲线
    数学图形(1.9)悬链线
    数学图形(1.8) 圆外旋轮线
    git 冲突解决
    Nginx配置文件(nginx.conf)配置详解
    【LNMP】提示Nginx PHP “No input file specified”错误的解决办法
    Windows如何压缩tar.gz格式
    LNMP安装目录及配置文件
  • 原文地址:https://www.cnblogs.com/yaowen/p/4889839.html
Copyright © 2011-2022 走看看