asserts文件存到外部SD卡里

Posted beens

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asserts文件存到外部SD卡里相关的知识,希望对你有一定的参考价值。

layout代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.wang.testapp3.ZuoyeActivity"
    android:orientation="vertical">
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#a1a1a1"
        android:id="@+id/iv_1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="asserts文件存到外部SD卡里"
        android:id="@+id/bt_1"/>


</LinearLayout>
View Code

Activity代码:

package com.example.wang.testapp3;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;

public class ZuoyeActivity extends AppCompatActivity {

    ImageView iv_1;
    Button bt_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zuoye);

        iv_1=(ImageView)findViewById(R.id.iv_1);
        bt_1=(Button)findViewById(R.id.bt_1);

        bt_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    AssetManager assetManager=getAssets();

                    InputStream inputStream = assetManager.open("sss.jpg");

                    FileOutputStream fos=openFileOutput("test3.jpg",MODE_PRIVATE);

                    byte[] b=new byte[1024];

                    int i=0;

                    while ((i=inputStream.read(b))>0)
                    {
                        fos.write(b,0,i);
                    }
                    fos.close();
                    inputStream.close();


                    String path=getFilesDir().getAbsolutePath()+"/test3.jpg";

                    Toast.makeText(ZuoyeActivity.this, "path="+path, Toast.LENGTH_SHORT).show();

                    Bitmap bm= BitmapFactory.decodeFile(path);

                    iv_1.setImageBitmap(bm);
                    Toast.makeText(ZuoyeActivity.this, "保存文件成功", Toast.LENGTH_SHORT).show();

                    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
                    {
                        try {

                            String path1=Environment.getExternalStorageDirectory()
                                    .getCanonicalPath();

                            Toast.makeText(ZuoyeActivity.this, "path1="+path1, Toast.LENGTH_LONG).show();

                            FileOutputStream fos1=new FileOutputStream(path1+"/test3.jpg");

                            PrintStream ps=new PrintStream(fos1);
                            ps.print(R.id.iv_1);
                            ps.close();
                            fos.close();

                            Toast.makeText(ZuoyeActivity.this, "写入外部文件成功", Toast.LENGTH_SHORT).show();
                        }
                        catch (Exception e)
                        {
                            Toast.makeText(ZuoyeActivity.this, "存储文件出错", Toast.LENGTH_SHORT).show();
                        }

                    }
                    else
                    {
                        Toast.makeText(ZuoyeActivity.this, "SD卡没有挂载", Toast.LENGTH_SHORT).show();
                    }


                }
                catch (Exception e)
                {
                    Toast.makeText(ZuoyeActivity.this, "保存文件出错", Toast.LENGTH_SHORT).show();

                }



            }
        });



    }
}
View Code

 

以上是关于asserts文件存到外部SD卡里的主要内容,如果未能解决你的问题,请参考以下文章

手机sd卡里多了个msf文件夹,是啥软件生成的?手机只装了QQ音乐和手机QQ

用FATFS在SD卡里写一串数字

SD卡里有个Android文件夹,里面的东西能删吗? 都400+M了,有一些是游戏的数据包 但是我不知道啊!!

ARCGIS保存服务定义文件(.sd)后到服务器上发布服务

安卓Android系统手机软件怎么装到SD卡上

怎么找回tf卡里面的资料?