Internal storage to external storage

Source: Internet
Author: User

Java

Package Com.example.lenovo.lianxiyong;import Android.content.res.assetmanager;import Android.graphics.Bitmap; Import Android.graphics.bitmapfactory;import Android.os.environment;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.view.view;import Android.widget.imageview;import Android.widget.toast;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.inputstream;import Java.io.printstream;import java.util.Enumeration;    public class Shoujicunchuactivity extends appcompatactivity {ImageView iv1;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_shoujicunchu);    iv1= (ImageView) Findviewbyid (R.ID.IV1);            public void bt_1 (View v) {try {Assetmanager Assetmanager = Getassets ();            InputStream InputStream = Assetmanager.open ("nnn.jpg"); FileOutputStream FileOutputStream = OpenfileoUtput ("Ttt.jpg", mode_private);            Byte[]b=new byte[1024];            int i=0;            while ((I=inputstream.read (b)) >0) {fileoutputstream.write (b,0,i);            } fileoutputstream.close ();            Inputstream.close ();        Toast.maketext (Shoujicunchuactivity.this, "saved successfully", Toast.length_short). Show (); } catch (Exception e) {toast.maketext (Shoujicunchuactivity.this, "Save Failed", Toast.length_short). Sho        W (); }}//public void bt_2 (View v)//{//String Path=getfilesdir (). GetAbsolutePath () + "ttt.jpg";//Bitma P bm= bitmapfactory.decodefile (path),//} public void Bt_3 (View v) {if (Environment.getexternalstoragesta            Te (). Equals (environment.media_mounted)) {String path=getfilesdir (). GetAbsolutePath () + "ttt.jpg";            Bitmap bm= bitmapfactory.decodefile (path);                try {String str = bm.tostring (); String STR1 = Getexternalfilesdir ("Test"). Getcanonicalpath ();                FileOutputStream fos=new FileOutputStream ("str1");                PrintStream ps=new printstream (FOS);                Ps.print (str);                Ps.close ();            Fos.close (); } catch (Exception e) {toast.maketext (Shoujicunchuactivity.this, "Save Failed", Toast.lengt            H_short). Show ();         }} else {Toast.maketext (shoujicunchuactivity.this, "SD card not mounted", toast.length_short). Show (); }    }}
View Code

Xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns: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.lenovo.lianxiyong.ShoujicunchuActivity"android:orientation= "vertical">    <ImageViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/iv1"android:src= "@drawable/nnn"/>    <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Save asset file to internal storage"Android:onclick= "Bt_1"/>    <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Go to external memory"Android:onclick= "Bt_3"/></LinearLayout>
View Code

Internal storage to external storage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.