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();
}
}
});
}
}