English 中文(简体)
制作剪辑,并在安乐施室下载
原标题:Create csv file and download it in Android Studio
  • 时间:2023-10-04 15:20:54
  •  标签:
  • java
  • android

I m using Android Studio for a project and i have a problem. I have a list of item, and if i click to a button for each item i called "downloadCsvFile" function and i pass the item.
I want that this function create a csv file and write the item data in it, and then download the file on the device. How can i do it? Thanks! :)

findViewById(R.id.buttonCsv).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                for (Item i: items) {
                    downloadCsvFile(i);
                }
            }

});
问题回答

查询以下密码,以制作CSV文档,并在Antoinroom下载。

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;
import android.app.DownloadManager; 

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_WRITE_EXTERNAL_STORAGE = 1;

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

        findViewById(R.id.buttonCsv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity.this,
                            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                            REQUEST_WRITE_EXTERNAL_STORAGE);
                } else {
                    for (Item item : items)
                        downloadCsvFile(item);
                }
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_WRITE_EXTERNAL_STORAGE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                for (Item item : items) 
                    downloadCsvFile(item);
            } else {
                showToast("Permission denied. Cannot download CSV files.");
            }
        }
    }

    private void downloadCsvFile(Item item) {
       try {
            String url = item.getURL(); // Ex: https://media.githubusercontent.com/media/datablist/sample-csv-files/main/files/organizations/organizations-100.csv
            manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
            Uri uri = Uri.parse(url); 
            DownloadManager.Request request = new DownloadManager.Request(uri); 
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); 
            long reference = manager.enqueue(request); 
            showToast("CSV file downloaded successfully");

        } catch (Exception e) {
            e.printStackTrace();
            showToast("Error creating CSV file");
        }
    }

    private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}

确保增加必要的许可:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签