asserts文件存到外部SD卡里

时间:2021-03-29 09:45:03
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(); } }
}); }
}

java

<?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>

xml

asserts文件存到外部SD卡里asserts文件存到外部SD卡里asserts文件存到外部SD卡里asserts文件存到外部SD卡里asserts文件存到外部SD卡里asserts文件存到外部SD卡里asserts文件存到外部SD卡里asserts文件存到外部SD卡里asserts文件存到外部SD卡里