My Android studio App is not creating XL File in download folder in all mobiles

43 views Asked by At

I have created an app which takes input and produces a XL file. There are some problems which are given below:

Problem 1. The file is created in the download folder of internal storage and can be seen by the file manager of droid max2 , but my laptop does show that file unless I reboot my mobile or move the file in another folder.

problem 2. This file is not created by motog9. motog9 has no sd card.

My Codes are given below;



import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity {

    private EditText editTextExcel;
    public File filePath = new File(Environment.getExternalStorageDirectory() + "/Download/mynew2.xlsx");



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);

        editTextExcel = findViewById(R.id.editText);
    }

    public void buttonCreateExcel(View view){
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        HSSFSheet hssfSheet = hssfWorkbook.createSheet("Custom Sheet");

        HSSFRow hssfRow = hssfSheet.createRow(0);
        HSSFCell hssfCell = hssfRow.createCell(0);

        hssfCell.setCellValue(editTextExcel.getText().toString());

        try {
            if (!filePath.exists()){
                filePath.createNewFile();
            }

            FileOutputStream fileOutputStream= new FileOutputStream(filePath);
            hssfWorkbook.write(fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
            if (fileOutputStream!=null){
                fileOutputStream.close();
            }




        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

If I do not use Download folder, the result is same.

0

There are 0 answers