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:orientation="vertical" tools:context="com.hanqi.cunchu.lianxi"> <ImageView android:layout_width="70dp" android:layout_height="70dp" android:id="@+id/iv"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="assets文件图片"/> <ImageView android:layout_width="70dp" android:layout_height="70dp" android:id="@+id/iv2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="assets转到内部存储并显示" android:onClick="bt"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内部存储转到外部" android:onClick="bt2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示外部存储" android:onClick="bt3"/> </LinearLayout>
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(); } } }