android-data storage of the internal phone file storage

Source: Internet
Author: User

I. Basic outline

1. Description:

1> application operation requires some large data or images can be stored inside the phone

2> file Type: arbitrary

3> Path:/data/data/packagename/files/

4> Delete this data file when uninstalling an app

5> can also set permissions for manipulating data files (same as sharedpreferences)

Second, practice

1>fileinputstream fis=openfileinput ("logo.png"); Read file

2>fileoutputstream fos=openfileoutput ("Logo.png", mode_private); Save File

3>file Filesdir=getfilesdir (); Get Files Folder object

4> the file under Operation asserts:

. Context.getassets () gets Assetmanager

.  InputStream open (filename); Read file

5> Loading picture files:

Bitmap bitmap=bitmapfactory.decodefile (String pathName); (drawable: Indicates that a picture object can be drawn)

  

Save diagram File:

1> get InputStream: Read assets under Logo.png

Assetmanager manager=getassets ();

2> reading files

InputStream Is=manager.open ("logo.png");

3> Get OutputStream:/data/data/packagename/files/logo.png

FileOutputStream fos=openfileoutput ("Logo.png", context.mode_private);

4> Read Side write:

Byte[] Buffer=new byte[1024];

int len=-1;

while ((Len=is.read (buffer))!=-1) {

Fos.write (Buffer,0,len);

}

Fos.close ();

Is.close ();

To read a picture:

1> Get picture path:/data/data/packagename/files

String Filespath=getfiledir (). GetAbsolutePath ();

String imgpath=filespath+ "/logo.png";

2> loading picture file to get Bitmap object:

Bitmap Bitmap=bitmapfactory.decodefile (Imgpath);

3> set it to ImageView display:

Iv_if.setimagebitmap (bitmap);

Third, the source code

Save Picture:

Assetmanager manager=getassets ();
InputStream Is=manager.open ("logo.png");
FileOutputStream fos= openfileoutput ("Logo.png", context.mode_private);
Byte[] Buffer=new byte[1024];
int len=-1;
while ((Len=is.read (buffer))!=-1) {
Fos.write (buffer, 0, Len);
}
Fos.close ();
Is.close ();
Toast.maketext (Mainactivity.this, "saved successfully", Toast.length_short). Show ();

To read a picture:

android-data storage of the internal phone file storage

Related Article

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.