Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

File Save code is not saving file in internal storage

OP ariez4u

1st June 2014, 10:01 AM   |  #1  
OP Junior Member
Thanks Meter: 0
 
26 posts
Join Date:Joined: Jun 2012
i want to save a layout (relative layout), when i run the app everything is working fine, but i am unable to find saved file/s in the internal storage of the phone. Any help would be appreciated as i am new and unable to sort out what is going wrong . here is my code



Code:
public void saveMe(View v) {
    // get prompts.xml view
    LayoutInflater li = LayoutInflater.from(this);
    View promptsView = li.inflate(R.layout.prompt, null);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);

    // set prompt.xml to alertdialog builder
    alertDialogBuilder.setView(promptsView);

    final EditText userInput = (EditText) promptsView
            .findViewById(R.id.editTextDialog);


    // set dialog message
    alertDialogBuilder.setCancelable(false);
    alertDialogBuilder.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

            final String fileName = userInput.getText().toString();
            final View view1=findViewById(R.id.relativeLayout); // The view that you want to save as an image

            Bitmap bitmap = Bitmap.createBitmap(view1.getWidth(), view1.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(bitmap);
            view1.draw(c);

            if(fileName.length() == 0)

                Toast.makeText(EidCardFinal.this,"Please Enter File Name",Toast.LENGTH_SHORT).show();

            else{

            File file = new File(context.getFilesDir(), fileName);

            if (file.exists())
                Toast.makeText(EidCardFinal.this,"File Already Exists",Toast.LENGTH_SHORT).show();
            else{
                try{
                    FileOutputStream out = new FileOutputStream(file);
                    bitmap.compress(CompressFormat.PNG, 100, out);
                    Toast.makeText(EidCardFinal.this,"File Saved",Toast.LENGTH_SHORT).show();
                    out.close();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }

            }

            }

        }
    });
    alertDialogBuilder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();

    // show it
    alertDialog.show();

}
7th June 2014, 01:01 AM   |  #2  
ExoComet's Avatar
Junior Member
Flag Austin
Thanks Meter: 0
 
6 posts
Join Date:Joined: Jun 2014
Try adding a line:

out.flush();

just before calling close() on the FileOutputStream. I think this will get the save code to work, and it's worth trying.
8th June 2014, 06:09 PM   |  #3  
OP Junior Member
Thanks Meter: 0
 
26 posts
Join Date:Joined: Jun 2012
Quote:
Originally Posted by ExoComet

Try adding a line:

out.flush();

just before calling close() on the FileOutputStream. I think this will get the save code to work, and it's worth trying.

thanks for the suggestion, but i have opted for the external storage and its working fine
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes