How to read a file from internal storage or external storage and store the file in a arrayList in android

↘锁芯ラ 提交于 2020-07-23 07:49:25

问题


My requirement is the end-user must be able to upload files into the application from internal or external storage and finally display the name of the file in the page.

Actual result: Now I've fetched the file name from the storage and displayed the name in my page.

Expected Result: The end user must be able to load image or video files from external or internal storage to the application and finally display their name in the page.

But don't have any idea about how to load read the file from storage and store it in a arrayList.

Code for fetching the file name

public class ServiceDetails extends AppCompatActivity {
    
    private Button next, attachment_one;
    private ImageButton attach_file;

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

        next = findViewById(R.id.submit);
        attachment_one = findViewById(R.id.attachmentOne);
        attach_file = findViewById(R.id.attachFile);

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ServiceDetails.this, ServiceAddress.class);
                startActivity(intent);
            }
        });

        attach_file.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(Build.VERSION.SDK_INT > Build.VERSION_CODES.M &&
                        checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 10001);
                }
                new MaterialFilePicker()
                        .withActivity(ServiceDetails.this)
                        .withRequestCode(1)
                        .withFilter(Pattern.compile(".*\\.(mkv|wmv|avi|mpeg|swf|mov|mp4|jpg|jpeg)$"))
                        .withHiddenFiles(true) // Show hidden files and folders
                        .start();
            }
        });

        attachment_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                attachment_one.setVisibility(View.INVISIBLE);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1 && resultCode == RESULT_OK) {
            String file_path = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
            String file_array[] = file_path.split("/");
            String file_name = file_array[file_array.length - 1];
            // Do anything with file

            if(attachment_one.getText().toString().isEmpty()) {
                attachment_one.setVisibility(View.VISIBLE);
                attachment_one.setText(file_name);
            } 
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 10001: {
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(ServiceDetails.this, "Permission granted", Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(ServiceDetails.this, "Permission not granted", Toast.LENGTH_LONG).show();
                    finish();
                }
            }
        }
    }
}

I'm new to android and kindly help me providing solution for this answer. Million thanks in advance!

Image showing file attachment option

来源:https://stackoverflow.com/questions/62876824/how-to-read-a-file-from-internal-storage-or-external-storage-and-store-the-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!