SoFunction
Updated on 2025-03-11

Save file to sdcard code instance in android

Save file to sdcard code instance in android

Updated: May 20, 2015 09:52:59 Submission: junjie
This article mainly introduces the example of saving files to sdcard code in Android. This article directly gives the implementation code. Friends who need it can refer to it.

Directly upload the code:

package ;

import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;

public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    
    //The file name and file content to be saved    String fileName = "";
    String content = "This is a test.";
    
    //Judge whether sdcard exists    String state = ();
    if((Environment.MEDIA_MOUNTED)) {
     //Get the SDCard directory     File sdcardPath = ();
     
     File file = new File(sdcardPath, fileName);
     FileOutputStream fos;
  try {
  fos = new FileOutputStream(file);
  (());
  ();
  } catch (FileNotFoundException e) {
  ();
  } catch (IOException e) {
  ();
  }
  (this, "Save successfully", Toast.LENGTH_SHORT).show();
    } else {
     (this, "Sdcard does not exist to get it cannot be written", Toast.LENGTH_SHORT).show();
    }
    
  }
}

  • android
  • document
  • save
  • sdcard

Related Articles

  • Android custom two-way progress bar implementation code

    This article mainly introduces the implementation code of Android custom two-way progress bar, which is very practical and valuable. Interested students will learn about it together.
    2017-09-09
  • Log unified management class implemented by Android development

    This article mainly introduces the unified log management class implemented by Android development, involving Android log management and method reloading and other related operation techniques. Friends who need it can refer to it
    2017-12-12
  • Android recording--Use of AudioRecord and MediaRecorder

    This article mainly introduces the use of Android recordings-AudioRecord and MediaRecorder. The editor thinks it is quite good. I will share it with you now and give you a reference. Let's take a look with the editor
    2017-02-02
  • A simple Android timing task

    This article mainly introduces a simple Android timing task in detail, which has certain reference value. Interested friends can refer to it.
    2017-06-06
  • Android input box and text box plus scrollview example

    This article mainly introduces the example of android input box and text box plus scroll view. Friends who need it can refer to it.
    2014-05-05
  • Android studio exports APP test package and builds official signature package

    Hello everyone, this article mainly talks about Android studio export APP test package and building official signature package. Interested students should come and have a look. If it is helpful to you, remember to bookmark it.
    2021-12-12
  • Question about ListView nesting GridView in Android

    During Android development, the scenario where listview nests gridviews may be used, but there may be problems during the nesting process. Let’s take a look at what the problem is and how to solve it.
    2016-08-08
  • An example of Android using direction sensors to obtain a mobile phone's relative angle

    The following is an example to introduce to you the relative angle of Android using direction sensors to obtain mobile phones. Those who don’t know can refer to it.
    2013-06-06
  • Flutter uses Hero components to implement animations of custom path effects

    This article introduces how to use the createRectTween property of the Hero animation component to implement custom path effects animation. The sample code in the article is explained in detail. If you are interested, you can learn about it.
    2022-06-06
  • Introduction to the basic introduction of InetAddress and source code examples for Android development

    This article mainly introduces the introduction to the basic introduction of InetAddress for Android development. Friends who need it can refer to it.
    2020-03-03

Latest Comments