layout
java代码
package com.hanqi.cunchu;import android.content.res.AssetManager;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.ImageView;import android.widget.Toast;import java.io.FileOutputStream;import java.io.InputStream;public class lianxi extends AppCompatActivity { ImageView iv, iv2; int i=0; byte []b=new byte[1024]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.lianxi); iv=(ImageView)findViewById(R.id.iv); iv2=(ImageView)findViewById(R.id.iv2); } public void bt(View v){ try { AssetManager assetManager = getAssets(); InputStream inputStream = assetManager.open("aa.jpg"); String path=getExternalFilesDir("Music").toString(); Toast.makeText(lianxi.this, path, Toast.LENGTH_LONG).show(); FileOutputStream fos1=openFileOutput("b.jpg", MODE_PRIVATE); FileOutputStream fos=new FileOutputStream(path+"a.jpg"); while ((i=inputStream.read(b))>0){ fos1.write(b,0,i); fos.write(b,0,i); } fos1.close(); fos.close(); inputStream.close(); String path1=getFilesDir().getCanonicalPath()+"/b.jpg"; Bitmap b= BitmapFactory.decodeFile(path1); iv.setImageBitmap(b); }catch(Exception e){ e.printStackTrace(); } } public void bt2(View view){ return;// if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){// try{// String path=getExternalFilesDir("Music").toString();// Toast.makeText(lianxi.this, path, Toast.LENGTH_LONG).show();//// FileOutputStream fos=new FileOutputStream(path+"a.jpg");// PrintStream ps=new PrintStream(fos);// String s=new String(b,0,i);// ps.print(s);// ps.close();// fos.close();// Toast.makeText(lianxi.this, "写入完成", Toast.LENGTH_SHORT).show();//// }catch (Exception e){// e.printStackTrace();// }// }// else{// Toast.makeText(lianxi.this, "没有SD卡", Toast.LENGTH_SHORT).show();// } } public void bt3(View v){ try{ String path=getExternalFilesDir("Music").getCanonicalPath()+"a.jpg"; Bitmap b=BitmapFactory.decodeFile(path); iv2.setImageBitmap(b); } catch (Exception e){ e.printStackTrace(); } }}