- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用一个应用程序创建一个 csv 文件,然后我想导出并在手机上阅读。但是,我将其保存到的位置不可见,并且难以传输。
有没有办法将其保存到更容易访问的位置,例如电话上的/documents ?
(我对 Java 很陌生,如果这是一个明显的问题,我很抱歉)
谢谢!
public void submit(View v)
{
String nline = System.getProperty("line.separator");
String fname = firstName.getText().toString() + ",";
String sname = surname.getText().toString() + ",";
String gender = genderSpin.getSelectedItem().toString() + ",";
String eaddress = email.getText().toString() + ",";
String mnum = mobile.getText().toString() + ",";
String fos = course.getText().toString() + ",";
String prole = proleSpin.getSelectedItem().toString();
FileOutputStream file = null;
if(fname.length() <= 1 || sname.length() <= 1 || eaddress.length() <= 1){
Toast.makeText(this, "Please enter all mandatory fields", Toast.LENGTH_SHORT).show();
}
else {
try {
file = openFileOutput(fileName, MODE_APPEND);
file.write(fname.getBytes());
file.write(sname.getBytes());
file.write(gender.getBytes());
file.write(eaddress.getBytes());
if (mnum.length() < 11) {
mnum = "null,";
file.write(mnum.getBytes());
}
if (fos.length() <= 1) {
fos = "null,";
file.write(fos.getBytes());
}
file.write(prole.getBytes());
file.write(nline.getBytes());
firstName.getText().clear();
surname.getText().clear();
genderSpin.setSelection(0);
email.getText().clear();
mobile.getText().clear();
course.getText().clear();
proleSpin.setSelection(0);
Toast.makeText(this, "Successfully Submitted", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (file != null) {
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
最佳答案
先加WRITE_EXTERNAL_STORAGE
到您的 list 文件。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
if(!checkPermission()){
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
WRITE_PERMISSION);
}
}
else {
// Permission already granted
}
MainActivity.class
package co.introtuce.nex2me.writefle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class MainActivity extends AppCompatActivity {
public static final int WRITE_PERMISSION=0xff;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.mid);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestPermission();
}
});
}
public void requestPermission(){
if(!checkPermission()){
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
WRITE_PERMISSION);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
else {
afterPermisiion();
}
}
public boolean checkPermission(){
if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == WRITE_PERMISSION){
if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
//Permisiion Granted
afterPermisiion();
return;
}
}
requestPermission();
}
public void afterPermisiion(){
submit();
}
public boolean saveFile(String csv_contents, Context context){
OutputStream outputStream = null;
try{
File root = Environment.getExternalStorageDirectory();
if(root == null){
Log.d("SAVE_PHONE", "Failed to get root");
return false;
}
// create a directory
File saveDirectory = new File(root,"appName/files/csv" );
// create direcotory if it doesn't exists
// create direcotory if it doesn't exists
if(!saveDirectory.exists()) if ( !saveDirectory.mkdirs()){
Toast.makeText(context,"sorry could not create directory"+saveDirectory.getAbsolutePath(), Toast.LENGTH_LONG).show();
return false;
}
outputStream = new FileOutputStream( saveDirectory + "myfile.csv"); // filename.png, .mp3, .mp4 ...
if(outputStream != null){
Log.e( "SAVE_PHONE", "Output Stream Opened successfully");
}
byte[] bytes = csv_contents.getBytes();
outputStream.write( bytes, 0, bytes.length );
outputStream.close();
return true;
}catch (Exception e){
Log.d("EXCEPTION_IN",e.toString());
return false;
}
}
public void submit()
{
String nline = System.getProperty("line.separator");
String fname = "Name" + ",";
String sname = "Surname" + ",";
String gender = "gn" + ",";
String eaddress = "email" + ",";
String mnum = "num" + ",";
String fos = "fos" + ",";
String prole = "prole";
FileOutputStream file = null;
if(fname.length() <= 1 || sname.length() <= 1 || eaddress.length() <= 1){
Toast.makeText(this, "Please enter all mandatory fields", Toast.LENGTH_SHORT).show();
}
String csv_contents = nline+""+fname+sname+gender+eaddress+mnum+fos+prole;
if(saveFile(csv_contents,this)){
//File has saved
// DO something
Toast.makeText(this,"File has been saved",Toast.LENGTH_LONG).show();
}
else{
//Could not save file
// DO something
}
}
}
/storage/emulated/0/appName/files/csv
地点。您可以修改此位置。
关于java - 安卓工作室 : file written to another directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59627011/
我是一名学生,目前正在学习使用 R(工作室)工作,为此我收到了一项任务。我应该比较一些主要是随机生成的数据并从中得出结论。 然而,我遇到的问题是这个数据有一个 5 个级别的因子,我想一次比较一个级别的
当我今天启动我的 aptana 时,它无法启动。 以下是在 aptana studio 3 工作区/.metadata/log 中显示的错误 !SESSION 2011-10-25 10:16:4
我从我的一个 friend 那里听说了 cocoa 工作室,所以我很好奇想了解它的细节......据他说,内存处理的问题通过它的使用而减少了......现在首先我不知道它是什么。是第三方的框架SDK吗
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 已关闭 8 年前。 Improve
如何将库添加到我的项目中?我从这个网站下载了模块。 http://viewpagerindicator.com/#download . 我尝试将 gradle 文件更改为 buildscript {
在 aptana studio 3 中编辑片段的位置 我习惯使用 gedit 片段插件 当我进入命令菜单时 < html < 条件命令 < ie 条件命令它说未定义 有没有办法添加我们自己的片段以及如
我正在 android studio 中通过在线 Material 学习 Java 但是我被这个简单的程序困住了 Android Studio 说这段代码有错误,但我看不到它们,这是代码: packa
我只是想创建另一个线程,我需要让它继续执行某些操作,直到我单击按钮 A,然后在单击按钮 B 时恢复,仅此而已。 Thread t = new Thread(new Runnable(){ @Overr
抱歉,如果这个问题会非常模糊,但最近尝试在 Android 工作室上使用 LibGDX 并在 的指导下制作游戏 https://www.youtube.com/watch?v=rzBVTPaUUDg&
当我点击运行按钮时出现了这个错误 Error:org.gradle.api.artifacts.ResolveException: Could not resolve all dependencies
Image asset studio 提供旧版图标。其中之一是 Google Play Store 图标。起初,我以为在发布应用程序时,该图标已经可用,但必须在 Play 管理中心帐户上上传这个高分辨
我的 Flutter 应用在尝试运行时抛出错误。 Compiler message: org-dartlang-debug:synthetic_debug_expression:1:1: Erro
我的 ubuntu 工作室是一个 64 位系统,我已经安装了 Eclipse IDE,它在 java 开发中可以正常工作,但不能在 android 上工作 最佳答案 我认为它被称为“ia32-libs
我刚开始使用flutter进行移动开发,尝试运行默认的flutter程序时出现此错误。 No connected devices found; please connect a device, or
到目前为止它工作得很好,但我显然触摸了一些东西并且发生了一些事情。 Android Studio 没有找到 R.id.**** 和 R.layout.****。 我需要做什么来解决问题? 最佳答案 我
如何直接从 r studio 的 Amazon s3 读取 csv。我不能只使用 read_csv,如果我把, read_csv(url("s3a://abc/rerer.txt")) 我得到 网址错
我要通过电子邮件发送我的 .R 代码。 如果我的接收器也可以打开 .R 文件并折叠我的折叠函数,那就太好了。 (整洁易读) 这是如何在 Rstudio 中实现的? 最佳答案 根据 code foldi
我正试图在我的工作中从 bitbucket 导入一个 android 项目,因为前 android 开发人员退出了,我将继续他的工作。 Gradle 同步永远不会返回正常。它得到了错误: Error:
我正在尝试将 YELP API 集成到 Android Studio 的应用程序中。我正在尝试使用 Postman 中的发布请求获取访问 token 。但是,当我发送 post 请求时,我收到以 JS
请我想知道当我们给 Nibobee 机器人提供电机速度时,我们在 AVR studio 中使用的单位是什么: motpwm_setLeft(1500); 1500 单位是多少? 最佳答案 根据the
我是一名优秀的程序员,十分优秀!