I made an App that creates a *.csv file inside a folder (made by the same app) and at the same time is inside the Android's Download Folder.
The file can be shared with a ShareIntent, the problem is when the app runs in Android Oreo and the user tries to create the *.csv file, the apps crashes, the thrown exception is:
android.os.FileUriExposedException:
at android.os.StrictMode.onFileUriExposed (StrictMode.java:1958)
at android.net.Uri.checkFileUriExposed (Uri.java:2348)
at android.content.ClipData.prepareToLeaveProcess (ClipData.java:941)
at android.content.Intent.prepareToLeaveProcess (Intent.java:9735)
at android.content.Intent.prepareToLeaveProcess (Intent.java:9741)
at android.content.Intent.prepareToLeaveProcess (Intent.java:9720)
at android.app.Instrumentation.execStartActivity (Instrumentation.java:1795)
at android.app.Activity.startActivityForResult (Activity.java:4495)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult (BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult (FragmentActivity.java:79)
at android.app.Activity.startActivityForResult (Activity.java:4453)
at android.support.v4.app.FragmentActivity.startActivityForResult (FragmentActivity.java:859)
at android.app.Activity.startActivity (Activity.java:4814)
at android.app.Activity.startActivity (Activity.java:4782)
at com.bpl.spart.bloodpressurelogbook.MainActivity$createReportTask.onPostExecute (MainActivity.java:517)
at com.bpl.spart.bloodpressurelogbook.MainActivity$createReportTask.onPostExecute (MainActivity.java:420)
I know that I should use FileProvider to create Uri's, in API level > 24,but how Can I use the FileProvider to give access from a file inside the Downloads folder?
This is my current code:
Intent ShareIntent = new Intent(android.content.Intent.ACTION_SEND);
ShareIntent.setType("text/csv");
ShareIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {getString(R.string.reporteTomasDePresion)});
ShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, nombreReporte);
ShareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
startActivity(Intent.createChooser(ShareIntent, getString(R.string.compartirReporte)));
Where "f" is the reference to the .csv file.
public File getDocumentsStorageDir(String directorio){
File file = new File(getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS),directorio);
if(!file.mkdir()){
Log.e("StatusArchivo","Directorio no creado");
}
return file;
}
File file = getDocumentsStorageDir(REPORTDIR);
Calendar cal = Calendar.getInstance();
//sdf = new SimpleDateFormat("dd-MM-yyyy-HH-mm");
nombreReporte = "BPReport"+sdf.format(cal.getTime())+".csv";
f = new File(file.getAbsolutePath(),nombreReporte);
String csvFIle = "";
String sistolicaS;
String diastolicaS;
String pulsoS;
String posicionS;
String extremidadS;
String notaS;
String fechaS;
if(params[0].getCount() > 0 ) {
try {
OutputStreamWriter fout = new OutputStreamWriter(new FileOutputStream(f), StandardCharsets.ISO_8859_1);
String nombre = getString(R.string.Nombre);
String nombreDeUsuario = sharedPref.getString("nombreDelUsuario","");
String edad = getString(R.string.Edad);
String edadUsuario = sharedPref.getString("edadDelUsuario","");
String peso = getString(R.string.Peso);
String pesoUsuario = sharedPref.getString("pesoDelUsuario","");
String UnidadDePeso = sharedPref.getString("unidadPeso",getString(R.string.kilos));
String enfermedades = getString(R.string.Enfermedad);
String enfermedadesUsuario = sharedPref.getString("enfermedadesDelUsuario","");
String linea1 = nombre+": ," + nombreDeUsuario + "\n";
fout.write(linea1);
String linea2 = edad+": ," + edadUsuario + "\n";
fout.write(linea2);
String linea3 = peso+": ," + pesoUsuario + UnidadDePeso + "\n";
fout.write(linea3);
String linea4 = enfermedades+": ," + enfermedadesUsuario + "\n";
fout.write(linea4);
String tituloColumnas = getString(R.string.presionsistolica) + "," + getString(R.string.presiondiastolica) + "," + getString(R.string.pulsotil) + "," + getString(R.string.posiciontil) + "," + getString(R.string.extremidadtil) + "," + getString(R.string.notatil) + "," + getString(R.string.fechatil) + "\n";
fout.write(tituloColumnas);
params[0].moveToFirst();
if (params[0].moveToFirst()) {
for (int j = 0; j < params[0].getCount(); j++) {
sistolicaS = params[0].getString(params[0].getColumnIndex(Contrato.Columnas.SISTOLICA));
diastolicaS = params[0].getString(params[0].getColumnIndex(Contrato.Columnas.DIASTOLICA));
pulsoS = params[0].getString(params[0].getColumnIndex(Contrato.Columnas.PULSO));
posicionS = params[0].getString(params[0].getColumnIndex(Contrato.Columnas.POSICION));
extremidadS = params[0].getString(params[0].getColumnIndex(Contrato.Columnas.EXTREMIDAD));
notaS = params[0].getString(params[0].getColumnIndex(Contrato.Columnas.NOTA));
fechaS = params[0].getString(params[0].getColumnIndex(Contrato.Columnas.FECHA));
Log.v("CSV1", csvFIle);
csvFIle = "\"" + sistolicaS + "\",\"" + diastolicaS + "\",\"" + pulsoS + "\",\"" + posicionS + "\",\"" + extremidadS + "\",\"" + notaS + "\",\"" + fechaS + "\"\n";
Log.v("CSV2", csvFIle);
fout.write(csvFIle);
params[0].moveToNext();
}
}
fout.close();
c.close();
I found the answer by my self, In my paths.xml file I added:
<?xml version="1.0" encoding="utf-8"?>
<paths
xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="Descarga"
path="Download/"/>
<files-path name="nombre"
path="reportes" />
</paths>
And in the Java:
I changed:
ShareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
startActivity(Intent.createChooser(ShareIntent,getString(R.string.compartirReporte)));
For:
Uri fileUri = FileProvider.getUriForFile(MainActivity.this,"com.bpl.spart.bloodpressurelogbook.fileprovider",f);
ShareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
startActivity(Intent.createChooser(ShareIntent, getString(R.string.compartirReporte)));
Also the FileUriExposedException problem in Android Oreo is gone now.
Related
Hello i want to make a http server with nanohttpd that shows installed apps, and convert them to apk and download. I can list but how can i download the app i select
List<ApplicationInfo> packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA;
for (ApplicationInfo packageInfo : packages) {
String name = String.valueOf(packageManager.getApplicationLabel(packageInfo));
if (name.isEmpty()) {
name = packageInfo.packageName;
}
Drawable icon = packageManager.getApplicationIcon(packageInfo);
String apkPath = packageInfo.sourceDir;
apps.add(new App(name, apkPath, icon, packageInfo.packageName));
}
if (method.equals(Method.GET)) {
for (App file : apps) {
answer += "<a href=\"" + file.getAppName()
+ "\" alt = \"\">" + file.getAppName()
+ "</a><br>";
}
Ok, I basicly take the url after list the apps and check which appname equal to url after that i start to download it like that
if (method.equals(Method.GET)) {
if (a) {
answer = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; " +
"charset=utf-8\"><title> HTTP File Browser</title>";
for (App file : appShare) {
answer += "<a href=\"" + file.getAppName()
+ "\" alt = \"\">" + file.getAppName()
+ "</a><br>";
}
answer += "</head></html>";
if(blabla.equals(header.get("referer"))){
a=false;
}
} else {
for (int i = 0; i < appShare.size(); i++) {
if (appShare.get(i).getAppName().equals(appnames))
arrayC = i;
}
answer = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; " +
"charset=utf-8\"><title> Download " + appShare.get(arrayC).getAppName() + "</title>";
// serve file download
InputStream inputStream;
Response response = null;
if (mStatusUpdateListener != null) {
mStatusUpdateListener.onDownloadingFile(
new File(appShare.get(arrayC).getApkPath()), false);
}
try {
inputStream = new FileInputStream(
new File(appShare.get(arrayC).getApkPath()));
response = new DownloadResponse(
new File(appShare.get(arrayC).getApkPath()), inputStream);
} catch (Exception e) {
e.printStackTrace();
} response.addHeader(
"Content-Disposition", "attachment; filename=" + appShare.get(arrayC).getAppName()+".apk");
return response;
}
This is my exact code for editing the record in my text file.
My codes are run successfully, but the records within text file doesn't update once I clicked btnModify. What's wrong with my codes?
P.s: These codes are used for my awt program.
btnModify.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
ArrayList<String> tempData = new ArrayList<>();
String requestedID = txtID.getText().trim().toLowerCase();
String requestedName = txtName.getText().trim();
String requestedGender = txtGender.getText().trim();
String requestedContactNumber = txtContactNumber.getText().trim();
System.out.println("123");
try {
FileReader fr = new FileReader("test.txt")){
Scanner reader = new Scanner(fr);
System.out.println("123456");
while (reader.hasNext()){
String line = reader.nextLine();
String lineLtrim = line.toLowerCase().replaceAll("^\\{", "");
String lineRtrim = lineLtrim.replaceAll("\\}$", "");
String[] data = lineRtrim.split(",");
System.out.println("5566");
if(data[0].equals(requestedID)){
System.out.println("9999");
data[1] = data[1].replace(data[1], requestedName);
data[2] = data[2].replace(data[2], requestedGender);
data[3] = data[3].replace(data[3], requestedContactNumber);
tempData.add(data[0] + "," + data[1] + "," + data[2] + "," + data[3] );
outStream.write("{" + txtID.getText().replace(data[0], requestedID));
outStream.write("," + txtName.getText().replace(data[1], requestedName));
outStream.write("," + txtGender.getText().replace(data[2], requestedGender));
outStream.write("," + txtContactNumber.getText().replace(data[3], requestedContactNumber) + "}");
outStream.write("\r\n");
outStream.close();
System.out.println("632");
System.out.println(tempData);
JOptionPane.showMessageDialog(null, "Modify Successful!");
}
else {
tempData.add(data[0] + ":" + data[1] + ":" + data[2] + ":" + data[3]);
System.out.println("142");
System.out.println(tempData);
}
}
fr.close();
}
catch (Exception ex){
}
}
});
This is my text file format.
{001,Jack,male,0123456789}
{002,Mark,male,0198765432}
I am uploading image from url on ftp by this code.
Image uploaded successful but when i try to resize uploaded image get error as the follows.
String imageUrl = "http://www.totaldesign.ir/wp-content/uploads/2014/11/hamayesh.jpg";
FTPClient ftpClient = new FTPClient();
ftpClient.connect(ftp_server, ftp_port);
ftpClient.login(ftp_user, ftp_pass);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
// APPROACH #2: uploads second file using an OutputStream
URL url = new URL(imageUrl);
//**************select new name for image***********/
//get file extention
File file = new File(imageUrl);
String ext = getFileExtension(file);
//get file name from url
String fileName = imageUrl.substring(imageUrl.lastIndexOf('/') + 1, imageUrl.length());
String fileNameWithoutExtn = fileName.substring(0, fileName.lastIndexOf('.'));
//create new image name for upload
String img_name = "simjin.com_" + fileNameWithoutExtn;
//get current year time for image upload dir
Date date = new Date();
DateFormat yeae = new SimpleDateFormat("yyyy");
String current_year = yeae.format(date);
//create dirs if not exist
ftpClient.changeWorkingDirectory(current_year);
int dirCode = ftpClient.getReplyCode();
if (dirCode == 550) {
//create dir
ftpClient.makeDirectory(current_year);
System.out.println("created folder: " + current_year);
}
//get current month time for image upload dir
DateFormat month = new SimpleDateFormat("MM");
String current_month = month.format(date);
//create dirs if not exist
ftpClient.changeWorkingDirectory("/" + current_year + "/" + current_month);
dirCode = ftpClient.getReplyCode();
if (dirCode == 550) {
//create dir
ftpClient.makeDirectory("/" + current_year + "/" + current_month);
System.out.println("created folder: " + "/" + current_year + "/" + current_month);
}
String uploadDir = "/" + current_year + "/" + current_month;
//rename image file if exist
boolean exists;
String filePath = uploadDir + "/" + img_name + "." + ext;
exists = checkFileExists(filePath);
System.out.println("old file path=> " + exists);
//Rename file if exist
int i = 0;
while (exists) {
i++;
img_name = "simjin.com_" + fileNameWithoutExtn + i;
filePath = uploadDir + "/" + img_name + "." + ext;
exists = checkFileExists(filePath);
//break loop if file dont exist
if (!exists) {
break;
}
}
System.out.println("new file path=> " + filePath);
//set image name in array for return
result[0] = img_name;
//*************end select new name for image**********/
System.out.println("ftpClinet Replay Code=> " + ftpClient.getStatus());
//Start uploading second file
InputStream inputStream = new BufferedInputStream(url.openStream());
OutputStream outputStream = ftpClient.storeFileStream(filePath);
System.out.println("outputStream Status=> " + outputStream);
byte[] bytesIn = new byte[10240];
int read = 0;
while ((read = inputStream.read(bytesIn)) != -1) {
outputStream.write(bytesIn, 0, read);
}
inputStream.close();
outputStream.close();
boolean completed = ftpClient.completePendingCommand();
after success upload. I want to resize image by thumbnailator:
if (completed) {
System.out.println("The file is uploaded successfully.");
String new_img_name = uploadDir + "/" + img_name + "-150x150" + "." + ext;
OutputStream os = ftpClient.storeFileStream(filePath);
Thumbnails.of(image_url).size(150, 150).toOutputStream(os);
}
in this section get this error:
OutputStream cannot be null
Where is my wrong? And how to fix it?
I am trying to retrieve the full file path from the classes that have been edited. I can retrieve the comments, version number, author and date but can't seem to get the full file path that has been edited when more than one class has been included into the project.
I use these to retrieve the Comments and Author:
String comments = pc.getPendingChanges().getComment();
System.out.println("Comments: " + comments);
String author = pc.getPendingChanges().getAllPendingChanges()[0].getPendingSetOwnerDisplay();
System.out.println("Author: " + author);
I use this to retrieve the path:
String fileName = pc.getPendingChanges().getAllPendingChanges()[0].getLocalItem();
System.out.println("FileName: " + fileName);
but I only get this output:
FileName: C:\VS2013\Plugin\PluginTest\HelloWorld.classpath
I need to display the full path with the class name eg
FileName: C:\VS2013\Plugin\PluginTest\HelloWorld.Test.java
Full method displayed below:
#Override
public PolicyFailure[] evaluate(PolicyContext context)
throws PolicyEvaluationCancelledException {
final PendingCheckin pc = getPendingCheckin();
final List<PolicyFailure> failures = new ArrayList<PolicyFailure>();
final WorkItemCheckinInfo[] AssociatedWorkItems = pc.getWorkItems().getCheckedWorkItems();
WorkItem AssociatedCodeReview = null;
String failureReason = "";
for (int i=0; i<AssociatedWorkItems.length; i++) {
AssociatedCodeReview = AssociatedWorkItems[i].getWorkItem();
if (AssociatedCodeReview.getType().getName() == "Code Review") {
break;
}
}
if (AssociatedCodeReview != null) {
if (AssociatedCodeReview.getFields().getField("System.State").getValue()
.toString().equals("Not Approved")) {
failureReason = "Code Review Work Item associated but that is not approved by expert";
failures.add(new PolicyFailure(failureReason, this));
}
} else {
String fileName = pc.getPendingChanges().getAllPendingChanges()[0].getLocalItem();
System.out.println("FileName: " + fileName);
String fileName2 = pc.getPendingChanges().getAllPendingChanges()[0].getServerItem();
System.out.println("FileName2: " + fileName2);
String[] fileName3 = pc.getPendingChanges().getAffectedTeamProjectPaths();
System.out.println("FileName3: " + fileName3[0]);
//System.out.println("Found " + pc.getPendingChanges().getAffectedTeamProjectPaths()[0] + " work items.");
String comments = pc.getPendingChanges().getComment();
System.out.println("Comments: " + comments);
String author = pc.getPendingChanges().getAllPendingChanges()[0].getPendingSetOwnerDisplay();
System.out.println("Author: " + author);
String version = String.valueOf(pc.getPendingChanges().getAllPendingChanges()[0].getVersion());
System.out.println("Version: " + version);
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println("Date Time: " + dateFormat.format(date));
}
return failures.toArray(new PolicyFailure[failures.size()]);
}
It actually does work, it was an error with my Eclipse install.
String fileName = pc.getPendingChanges().getAllPendingChanges()[x].getLocalItem();
i have been searching the whole day for a solution but i'm unable to find one. That's why i decided to ask a question.
My problem is that i'm inserting a ellipse object to the PPTX file. When i tries to open the presentation MS OFfice says it's corrupted and tries to repair. It ends up deleting the slide and presents the master slide.
See below my code:
the function officeXReport.getTemplatename() contains the filename of the PPTX (e.g. *Status_Report_template.pptx*)
The template pptx file contains only 1 slide.
The function officeXReport.getNewfilename() contains the new filename of the PPTX (e.g. *Status_Report_2014-03-16.pptx*)
try {
PresentationMLPackage presentationMLPackage = (PresentationMLPackage)OpcPackage.load(new java.io.File(officeXReport.getTemplatename()));
MainPresentationPart pp = presentationMLPackage.getMainPresentationPart();
boolean noLine = false;
STShapeType st = STShapeType.ELLIPSE;
int i = 1;
SlidePart slidePart = (SlidePart)presentationMLPackage.getParts().getParts().get(pp.getSlide(0).getPartName());
Shape sample = ((Shape)XmlUtils.unmarshalString(
getPresetShape(st.value(), noLine, Long.toString(1839580), Long.toString(1314971), Long.toString(184337), Long.toString(184338), "92D050"), Context.jcPML) );
slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(sample);
presentationMLPackage.save(new java.io.File(officeXReport.getNewfilename()));
} catch (Exception ex) {
System.out.println("Exception thrown = " + ex.getMessage());
return null;
}
The related function getPresetShape():
private static String getPresetShape(String preset, boolean noLine, String x, String y, String cx, String cy, String colorcode) {
String txBody = "";
String ln = "";
String style = "";
if (!noLine) {
ln = "<a:ln w=\"3175\">"
+"<a:solidFill>"
+"<a:srgbClr val=\"000000\"/>"
+"</a:solidFill>"
+"</a:ln>";
style = "<p:style>\n" +
"<a:lnRef idx=\"1\">\n" +
"<a:schemeClr val=\"accent1\"/>\n" +
"</a:lnRef>\n" +
"<a:fillRef idx=\"3\">\n" +
"<a:schemeClr val=\"accent1\"/>\n" +
"</a:fillRef>\n" +
"<a:effectRef idx=\"2\">\n" +
"<a:schemeClr val=\"accent1\"/>\n" +
"</a:effectRef>\n" +
"<a:fontRef idx=\"minor\">\n" +
"<a:schemeClr val=\"lt1\"/>\n" +
"</a:fontRef>\n" +
"</p:style>";
txBody = "<p:txBody>\n" +
"<a:bodyPr rtlCol=\"false\" anchor=\"ctr\"/>\n" +
"<a:lstStyle/>\n" +
"<a:p>\n" +
"<a:pPr algn=\"ctr\"/>\n" +
"<a:endParaRPr lang=\"en-US\" sz=\"1100\" u=\"sng\" dirty=\"false\">\n" +
"<a:latin typeface=\"Futura Com Light\" panose=\"020B0402020204020303\" pitchFamily=\"34\" charset=\"0\"/>\n" +
"</a:endParaRPr>\n" +
"</a:p>\n" +
"</p:txBody>";
}
return
"<p:sp xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\">"
+ "<p:nvSpPr>"
+ "<p:cNvPr id=\"32\" name=\"Ellipse 2\" />"
+ "<p:cNvSpPr/>"
+ "<p:nvPr/>"
+ "</p:nvSpPr>"
+ "<p:spPr>"
+ "<a:xfrm>"
+ "<a:off x=\"" + x + "\" y=\"" + y + "\"/>"
+ "<a:ext cx=\"" + cx + "\" cy=\""+ cy + "\"/>"
+ "</a:xfrm>"
+ "<a:prstGeom prst=\"" + preset + "\">"
+ "<a:avLst/>"
+ "</a:prstGeom>"
+ "<a:solidFill>"
+ "<a:srgbClr val=\""+ colorcode + "\"/>"
+ "</a:solidFill>"
+ ln
+ "</p:spPr>"
+ style
+ txBody
+ "</p:sp>";
}
I have looked into the slide1.xml file which is missing the XML header compared to a correct slide1.xml file :
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
How can i get the xml header definition into the slide1.xml ?
Thanks,
Asad