Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7871333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:52:18+00:00 2026-06-03T01:52:18+00:00

Please check the code below. I am not able to copy a resource file

  • 0

Please check the code below.
I am not able to copy a resource file to external storage in android phone.
Though, i’m able to copy using the code i’ve commented but I want to know the problem with my original code.

public class ExternalData extends Activity implements OnItemSelectedListener,
    OnClickListener {

TextView canRead = null;
TextView canWrite = null;
String state = null;
boolean canR, canW;

EditText userEnteredFileName;
Button bConfirm, bSaveAs;

Spinner spinner = null;
String[] stuff = { "Music", "Pictures", "Downloads" };
File path = null;
File file = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.externaldata);

    canRead = (TextView) findViewById(R.id.tvCanRead);
    canWrite = (TextView) findViewById(R.id.tvCanWrite);

    userEnteredFileName = (EditText) findViewById(R.id.etUserEnteredFileName);
    bConfirm = (Button) findViewById(R.id.bConfirm);
    bSaveAs = (Button) findViewById(R.id.bSaveAs);

    bConfirm.setOnClickListener(this);
    bSaveAs.setOnClickListener(this);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, stuff);
    spinner = (Spinner) findViewById(R.id.spinner1);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {

    int position = spinner.getSelectedItemPosition();
    switch (position) {
    case 0:
        path = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
        break;
    case 1:
        path = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        break;
    case 2:
        path = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        break;
    }
}

public void checkState() {

    state = Environment.getExternalStorageState();
    if (state.equals(Environment.MEDIA_MOUNTED)) {
        canRead.setText("Can Read: true");
        canWrite.setText("Can Write: true");
        canR = canW = true;
    } else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        canRead.setText("Can Read: true");
        canWrite.setText("Can Write: false");
        canR = true;
        canW = false;
    } else {
        canRead.setText("Can Read: false");
        canWrite.setText("Can Write: false");
        canR = canW = false;
    }
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch (arg0.getId()) {
    case R.id.bConfirm:
        bSaveAs.setVisibility(View.VISIBLE);
        break;
    case R.id.bSaveAs:
        checkState();
        if(canR == canW == true){

            file = new File (path, userEnteredFileName.getText().toString() + "/");


            try {
                if(!file.exists()){
                    boolean success = file.mkdirs();
                    if(success){
                        Toast.makeText(ExternalData.this, "File created", Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(ExternalData.this, "Not able to create file", Toast.LENGTH_LONG).show();
                    }
                }
                InputStream fis = getResources().openRawResource(R.drawable.darthvader);
                /* this works....but wats wrong with the original code
                file = new File(file, "darthVader.png");
                if(!file.exists()){
                    file.createNewFile();
                }
                */
                FileOutputStream fos = new FileOutputStream(file);
                byte[] data = new byte[fis.available()];
                fis.read(data);
                fos.write(data);
                fis.close();
                fos.close();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                Toast.makeText(ExternalData.this, "FilNOT", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Toast.makeText(ExternalData.this, "IO", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }
        break;
    }
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:52:20+00:00Added an answer on June 3, 2026 at 1:52 am

    see this example for How to write file in sdcard (external storage)

            InputStream input;
            try {
                URL url = new URL (strURL);
                input = url.openStream();
                byte[] buffer = new byte[1500];
                OutputStream output = new FileOutputStream ("/sdcard/"+pos+".png");
    
    
                    int bytesRead = 0;
                    while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0)
                    {
                        output.write(buffer, 0, bytesRead);
                    }
    
                finally
                {
                    output.close();
                    buffer=null;
                }
    

    don’t forget to give permisson in manifeast file

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a weird problem here. Please check the code below. I do not
I am trying to generate images using ImageMagick, please check the code below: <?
Please check this below code. After for loop remaining code is not executing. It
check the code below please: static void Main(string[] args) { IList<dynamic> items = new
I have a batch which check the existence of a file. Please see code
Please check the code below: objDDLTable = HttpContext.Current.Cache[TestSet] as Hashtable; if (objDDLTable == null)
Please check the code below, the problem is that no form element are displayed
I am stuck in swich case. Please check code below Log.e(@@@@@@@@@@@@@@@@@@@@@ pos,+posSel_lay); String ff=Integer.toString(posSel_lay);
Please check the below code $(document).ready(function(){ var href; if(href!=null) { setContainerHtml($.cookie(activeElementHref)); }; $('nav ul
Folks, Please check my code..am executing the below code by http://localhost/mycart/login.php?is_ajax=1&username=srini&password=srini Then am getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.