File Save code is not saving file in internal storage

Search This thread

ariez4u

Member
Jun 30, 2012
44
1
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();

}
 

ExoComet

Member
Jun 6, 2014
6
0
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.