I'm working on an app on Android Studio and I would like to export data in an other app (gmail for example) when I click on a button.
This is my code:
btnSurveyExportD = (Button) findViewById(R.id.btnSurveyExportD);
btnSurveyExportD.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View v) {
Business x = new Business();
String queryBeforEncryption = x.toRHSToolbox(s);
queryBeforEncryption = queryBeforEncryption.replace("\n","\r\n");
queryBeforEncryption = queryBeforEncryption.replace("'MV'","Null");
queryBeforEncryption = queryBeforEncryption.replace("' '","Null");
queryBeforEncryption = queryBeforEncryption.replace("'_'","Null");
queryBeforEncryption = queryBeforEncryption.replace("'-9'","Null");
queryBeforEncryption = queryBeforEncryption.replace("=-9.0","=Null");
queryBeforEncryption = queryBeforEncryption.replace("=-9.00","=Null");
queryBeforEncryption = queryBeforEncryption.replace("=-9","=Null");
//String query = queryBeforEncryption;
String query = x.NWEncryption(queryBeforEncryption,key);
File file = null;
File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
if (root.canWrite()){
File dir = new File (root.getAbsolutePath() + "/QueryToDatabase");
dir.mkdirs();
file = new File(dir, "DataMobileRHS_" + s.getSite().toString() + ".rhs" );
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
out.write(query.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Uri u1 = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider",root);
//if the user took no photos
if(MobileRHS.survey.getAlbum_path()==null){
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Survey export files - " + s.getSite().toString());
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/html");
startActivity(sendIntent);
}
//else
else{
Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Survey export files - " + s.getSite().toString());
sendIntent.setType("*/*"); //all types are accepted
ArrayList<Uri> queryAndPhotos = new ArrayList<>();
queryAndPhotos.add(u1); //the text file is added
File[]filesToSend = new File (MobileRHS.survey.getAlbum_path()).listFiles();
for (File i : filesToSend) { //each photo is added
Uri uri = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", i);
queryAndPhotos.add(uri);
}
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, queryAndPhotos);
startActivity(sendIntent);
}
}
});
For each export I tried, the if(root.canWrite()) condition is not respected, so the last lines are not executed. How can I make my file root writable?
Thank you for advance
I had the same problem with File.exists(). Solved it by using getExternalFilesDir().
So I would recommend changing
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
to
Environment.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Not sure, that you have the same problem, but it's worth a try
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.