Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,739,676 Members 52,053 Now Online
XDA Developers Android and Mobile Development Forum

File Save code is not saving file in internal storage

Tip us?
 
ariez4u
Old
#1  
Junior Member - OP
Thanks Meter 0
Posts: 20
Join Date: Jun 2012
Default File Save code is not saving file in internal storage

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:
Select 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();

}
 
ExoComet
Old
#2  
ExoComet's Avatar
Junior Member
Thanks Meter 0
Posts: 6
Join Date: Jun 2014
Location: Austin
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.
 
ariez4u
Old
#3  
Junior Member - OP
Thanks Meter 0
Posts: 20
Join Date: Jun 2012
Quote:
Originally Posted by ExoComet View Post
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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes