Layout code:
<?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 file saved to external SD card"Android:id= "@+id/bt_1"/></linearlayout>
View Code
Activity code:
Packagecom.example.wang.testapp3;ImportAndroid.content.res.AssetManager;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.os.Bundle;Importandroid.os.Environment;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast;ImportJava.io.FileOutputStream;ImportJava.io.InputStream;ImportJava.io.PrintStream; Public classZuoyeactivityextendsappcompatactivity {ImageView iv_1; Button bt_1; @Overrideprotected voidonCreate (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 (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Try{Assetmanager Assetmanager=getassets (); InputStream InputStream= Assetmanager.open ("Sss.jpg"); FileOutputStream Fos=openfileoutput ("Test3.jpg", mode_private); byte[] b=New byte[1024]; intI=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, "Save file succeeded", 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=NewFileOutputStream (path1+ "/test3.jpg"); PrintStream PS=NewPrintStream (FOS1); Ps.print (r.id.iv_1); Ps.close (); Fos.close (); Toast.maketext (zuoyeactivity. This, "Write external file succeeded", Toast.length_short). Show (); } Catch(Exception e) {toast.maketext (zuoyeactivity. This, "Error storing file", Toast.length_short). Show (); } } Else{toast.maketext (zuoyeactivity). This, "SD card is not mounted", Toast.length_short). Show (); } } Catch(Exception e) {toast.maketext (zuoyeactivity. This, "Error saving file", Toast.length_short). Show (); } } }); }}
View Code
Asserts file to external SD card