File files in Android store and read bitmap in file

Source: Internet
Author: User

This year looked at the Android file storage, roughly the same as the Java implementation

Android provides the following two ways to open the IO stream of data files for this application:

FileInputStream Openfileinput (fileName); Input stream
FileOutputStream outputstream = Openfileoutput (FileName, mode_private); Output stream

The second way to open the output stream is to indicate the mode of the open file, which is about a few:

Mode_private: The file can only be read and written by the current program and is overwritten

Mode_append: Open the file as an append to add content to the file

Mode_world_readable: The contents of this file can be read by other applications

Mode_world_writeable: The contents of this file can be read and written by other programs

The following simple implementation reads the input text from a edittext and displays it in the TextView

Mainactivity.java

Package com.file.filetest;import java.io.bufferedreader;import java.io.bufferedwriter;import  java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream; import java.io.ioexception;import java.io.inputstreamreader;import java.io.outputstreamwriter; import java.io.unsupportedencodingexception;import com.file.filetest.r;import  android.app.activity;import android.os.bundle;import android.view.view;import  Android.view.view.onclicklistener;import android.widget.button;import android.widget.edittext;import  android.widget.textview;public class mainactivity extends activity {public  static String FILE_NAME =  "Testfile.bin";p rivate edittext edittext;private  textview textview;private button readbutton;private button writebutton;@ Overrideprotected void oncreate (Bundle savedinstancestate)  {super.oncreate (savedinstancestate); Setcontentview (R.layout.main);readButton =  ( button)  findviewbyid (r.id.button2);writebutton =  (Button)  findviewbyid (R.id.button1); edittext =  (EditText)  findviewbyid (R.ID.EDITTEXT1);textview =  (TextView)   Findviewbyid (R.id.textview1); Readbutton.setonclicklistener (New onclicklistener ()  {@Overridepublic  void onclick (view v)  {textview.settext (ReadFile (file_name));}); Writebutton.setonclicklistener (New onclicklistener ()  {@Overridepublic  void onclick (View  V)  {writefile (File_name, edittext.gettext (). toString ());}}); Private string readfile (String filename)  {bufferedreader breader = null;try  {fileinputstream instream = openfileinput (fileName); inputstreamreader reader =  new inputstreamreader (instream,  "GBK"); Breader = new buffeRedreader (reader); Stringbuffer stringbuffer = new stringbuffer (""); string str;while  ((Str = breader.readline ())  != null)  {stringBuffer.append ( STR);} Return stringbuffer.tostring ();}  catch  (filenotfoundexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  catch  (ioexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  finally {if  (Breader != null)  {try {breader.close ();}  catch  (ioexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}}} return  "";} Private void writefile (String filename, string context)  {BufferedWriter  writer = null; Fileoutputstream outputstream;try {outputstream = openfileoutput (fileName, MODE_ PRIVATE); outputstreamwriter outputwriter  = new outputstreamwriter (outputstream,  "GBK"); Writer = new bufferedwriter ( Outputwriter); Writer.write (context); Writer.newline (); Writer.flush ();}  catch  (filenotfoundexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  catch  (unsupportedencodingexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  catch  (ioexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  finally {if  (Writer != null)  {try {writer.close ();}  catch  (ioexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}}}}

Layout file Main.xml:

<relativelayout 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"     tools:context= "${ Relativepackage}.${activityclass} " >    <EditText         android:id= "@+id/edittext1"         android:layout_ Width= "Wrap_content"         android:layout_height= "Wrap_content"          android:layout_alignparentleft= "true"          android:layout_alignparenttop= "true"         android: Ems= "Ten"  >        <requestFocus />     </edittext> &Nbsp;  <textview        android:id= "@+id/textView1"          android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"         android: Layout_alignparentleft= "true"         android:layout_below= "@+id/ EditText1 "        android:text=" TextView " />     <button        android:id= "@+id/button1"          android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"         android:layout_ Alignparentleft= "true"         android:layout_below= "@+id/textView1"         android:layout_margintop= "98DP"         android:text= "write " />    <button        android:id=" @+id /button2 "        android:layout_width=" Wrap_content "         android:layout_height= "Wrap_content"          android:layout_alignbaseline= "@+id/button1"         android: Layout_alignbottom= "@+id/button1"         android:layout_ Centerhorizontal= "true"         android:text= "read"  /></ Relativelayout>

The layout is as follows:

The above basic implementation of the file read and write function, but this can only meet the input only text and number of cases, when the input image (such as input a smiley face), there is a problem, in the internet roughly searched, and did not find a better solution, text mixed file, there are 4 problems to solve:

    1. Identify input bitmap from EditText and record

    2. Save text and bitmap to file, bitmap to encode separately

    3. Parsing text messages and bitmap (decoding) from a file

    4. Text and pictures in TextView display from a file-solution using Spannablestring class

      Looking for answers from the great God



File files in Android store and read bitmap in file

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.