I'm making an android application, which saves data to a file in the settings activity.
I made some custom functions to ease writing my files, they're in a class all my activities inherit from, including the settings activity.
Custom functions:
public void WriteToFile(String filename, String tag, String value) {
try {
FileOutputStream fileOut = openFileOutput(filename + ".txt", MODE_PRIVATE);
OutputStreamWriter writer = new OutputStreamWriter(fileOut);
writer.write(ReadFile(filename + ".txt") + tag + ":" + value + ";");
writer.close();
} catch (Exception e) {
Toast.makeText(this, "ERROR: " + e.toString(), Toast.LENGTH_LONG).show();
Log.e("Error writing: ", e.toString());
}
}
public void WipeFile(String filename) {
try {
FileOutputStream fileOut = openFileOutput(filename + ".txt", MODE_PRIVATE);
OutputStreamWriter writer = new OutputStreamWriter(fileOut);
writer.write("");
writer.close();
} catch (Exception e) {
Toast.makeText(this, "ERROR: " + e.toString(), Toast.LENGTH_LONG).show();
Log.e("Error writing: ", e.toString());
}
}
public String ReadFile(String filename) {
try {
FileInputStream fileIn = openFileInput(filename + ".txt");
InputStreamReader InputRead = new InputStreamReader(fileIn);
char[] inputBuffer = new char[10000];
String content = "", readString;
int charRead;
while ((charRead = InputRead.read(inputBuffer)) > 0) {
readString = String.copyValueOf(inputBuffer, 0, charRead);
content += readString;
}
InputRead.close();
return content;
} catch (Exception e) { WipeFile(filename); return ""; }
}
public String FileValue(String filename, String tag, String defaultValue) {
String[] content = ReadFile(filename + ".txt").split(";");
for (String pair : content) {
if (pair.split(":")[0].equals(tag)) return pair.split(":")[1];
} WriteToFile(filename, tag, defaultValue); return defaultValue;
}
Settings activity:
#Override
#SuppressWarnings("ConstantConditions")
protected void onCreate(Bundle savedInstanceState) {
ToolbarTitle = "Settings";
ActivityID = R.layout.activity_settings;
ToolbarID = R.id.settings_toolbar;
ToolbarIcon = R.mipmap.settings_icon;
ActivityLayout = R.id.settings_layout;
super.onCreate(savedInstanceState);
if (prefs.getInt("LoggedinID", 0) == 0) findViewById(R.id.settings_user).setVisibility(View.GONE);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.settings_lowBattery, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
int spinnerPosition = adapter.getPosition(FileValue("settings", "Alert", "20%"));
Spinner battery = ((Spinner) findViewById(R.id.settings_battery));
battery.setAdapter(adapter);
battery.setSelection(spinnerPosition);
((Switch) findViewById(R.id.settings_notifications)).setChecked(FileValue("settings", "Notifications", "1").equals("1"));
findViewById(R.id.settings_ads).setVisibility((FileValue("settings", "Ads", "1").equals("1") ? View.VISIBLE : View.INVISIBLE));
}
#SuppressWarnings("ConstantConditions")
public void Apply(View view) {
WipeFile("settings");
WriteToFile("settings", "Notifications", (((Switch) findViewById(R.id.settings_notifications)).isChecked() ? "1" : "0"));
WriteToFile("settings", "Alert", ((Spinner) findViewById(R.id.settings_battery)).getSelectedItem().toString());
}
public void Ads(View view) {
Toast.makeText(this, "Just a prank, bro", Toast.LENGTH_SHORT).show();
WriteToFile("settings", "Ads", "0");
}
What's weird is that it all worked when it was messy and not in custom functions, any idea why?
The problem seems to occur in the ReadFile function, where InputRead.read(inputBuffer) returns -1 (No data in file).
I have no idea how to even check where the problem lies, when writing to the file or when reading from it....
Thanks ahead
PROBLEM SOLVED
1. The ReadFile function that was inside the writer.write function couldn't open the file and read it since the writer kept it open for itself.
2. That same ReadFile function was provided with (filename + ".txt"), and added ".txt" to it as well.
It seems like the problem might be in how you are appending to file...
Internally your write function opens the file, then before closing it, your read function opens the same file and closes it. It could be that either the read function is failing when it tries to open the file because it is already open, but not closed. OR it could be that when the read function closes the file it also closes the file for the write function...
So the problem seems to be that you want to append to the file in your write function, but you are implementing it poorly. You do not need to rewrite the contents to file. You just need to find the proper flag to open the file for appending.
You should use a java.util.Properties for your settings. It is like a Map<String, String>.
To load all your settings, use load(Reader reader).
To save all your settings, use save(OutputStream out, String comments).
Related
I have this account creation program I'm working on, and would love to save the persons name, last name, email and password to a text file. The following snippet should do just that, but the error message I'm getting when I put a String variable in the .write method is, "no suitable method found for write(JTextFeild)".
private void signupActionPerformed(java.awt.event.ActionEvent evt) {
fname.getText();
lname.getText();
email.getText();
reemail.getText();
password.getText();
repassword.getText();
if(male.equals(true)) {
males = true;
}
if(female.equals(true)) {
females = true;
}
BufferedWriter writer = null;
try {
writer = new BufferedWriter( new FileWriter("UserPass.txt"));
writer.write(fname);
}
catch ( IOException e) {
}
finally {
try {
if ( writer != null) {
writer.close( );
}
}
catch ( IOException e) {
}
}
}
Any ideas on how to fix this?
From the documentation of getText() in javax.swing.text.JTextComponent:
public String getText()
JTextField is just the GUI element, getText() doesn't change it.
You should store the result in a String variable and then use it to write().
I am trying to get all frames from an mp4 file using the ExtractMpegFrames.java class found here http://bigflake.com/mediacodec/ExtractMpegFramesTest.java.txt
What I currently do is create a temp file (File.createTempFile) in a directory that stores all the frames, create a FileOutputStream and do
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut)
where fOut is the OutputStream with the file.
Currently, the saved images look like this: https://imgur.com/a/XpsV2
Using the Camera2 Api, I record a video and save it as an mp4. According to VLC, the color space for the video is Planar 4:2:0 YUV Full Scale.
Looking around, it seems that each vendor uses different color spaces
https://stackoverflow.com/a/21266510/7351748. I know ffmpeg can conversions with color spaces, but I cannot use it.
I am not sure where to start to solve this issue of the strange output pngs. I am assuming that this is a color space issue, but I can be completely wrong here.
You can get all Frames of Video Using ffmpeg library here is working code.
add dependancy
compile 'com.writingminds:FFmpegAndroid:0.3.2'
to your gradle
private void loadFFMpegBinary() {
try {
if (ffmpeg == null) {
ffmpeg = FFmpeg.getInstance(this);
}
ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
// #Override
// public void onFailure() {
// showUnsupportedExceptionDialog();
// }
#Override
public void onSuccess() {
Log.d(TAG, "ffmpeg : correct Loaded");
}
});
} catch (FFmpegNotSupportedException e) {
} catch (Exception e) {
Log.d(TAG, "EXception : " + e);
}
}
here is image extratct method
public void extractImagesVideo() {
File moviesDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES
);
String filePrefix = "extract_picture";
String fileExtn = ".jpg";
String yourRealPath = getPath(Pick_Video.this, DataModel.selectedVideoUri);
Log.d("selected url", "" + DataModel.selectedVideoUri);
File src = new File(yourRealPath).getAbsoluteFile();
File appDir=new File(moviesDir,"/"+app_name+"/");
if(!appDir.exists())
appDir.mkdir();
DataModel.appDir=appDir;
File dir = new File(appDir, "testVideo");
int fileNo = 0;
while (dir.exists()) {
fileNo++;
dir = new File(moviesDir+"/"+app_name+"/", "testVideo" + fileNo);
}
dir.mkdir();
DataModel.dir = dir;
resultList = new ArrayList<String>(256);
filePath = dir.getAbsolutePath();
File dest = new File(dir, filePrefix + "%03d" + fileExtn);
Log.d(TAG, "startTrim: src: " + src.toString());
Log.d(TAG, "startTrim: dest: " + dest.getAbsolutePath());
String[] complexCommand = { "-i",""+src.toString(),"-qscale:v", "2","-vf", "fps=fps=20/1",dest.getAbsolutePath()};
//"-qscale:v", "2","-vf", "fps=fps=20/1",//
//works fine with speed and
execFFmpegBinary(complexCommand);
}
call this two method on button click event
Comment If Any query.
I have a code which is checking a defined type of audio file in folder and calling converter to change its format. Now when first file is passed, converter is called and as file is in process of being conversion, for loop called converter again for second file. In this i felt earlier/later process is terminated and hence i m getting only file converted as output. Code is here. How can i manage to get all files convereted.
public void convertAudio(View v) {
final File pathanme = new File(Environment.getExternalStorageDirectory() + "/sdcard/test");
File files[] = pathanme.listFiles();
for (File f : files) {
if (f.getName().endsWith(".mp4")) {
String filename = f.getName().toLowerCase().toString();
System.out.println(filename);
File wavFile = new File(pathanme, filename);
IConvertCallback callback = new IConvertCallback() {
#Override
public void onSuccess(File convertedFile) {
Toast.makeText(NewMainActivity.this, "SUCCESS: " + convertedFile.getPath(), Toast.LENGTH_LONG).show();
}
#Override
public void onFailure(Exception error) {
Toast.makeText(NewMainActivity.this, "ERROR: " + error.getMessage(), Toast.LENGTH_LONG).show();
}
};
Toast.makeText(this, "Converting audio file..." + filename, Toast.LENGTH_SHORT).show();
AndroidAudioConverter.with(this)
.setFile(wavFile)
.setFormat(AudioFormat.MP3)
.setCallback(callback)
.convert();
}
}
If u see there is success message against conversion and i never got this under for loop whereas if i pass only one file, i got success message. pls advice.
You could add a class instance variable for an index and increment it as necessary, calling the convert() method recursively as necessary. It'd look something like this (Java is a little rusty, you may have to clean up syntax):
public class MyClass {
private int fileIndex = 0;
private File[] files;
public void convertAudio(View v) {
final File pathanme = new File(Environment.getExternalStorageDirectory() + "/sdcard/test");
this.files = pathanme.listFiles();
fileIndex = 0;
convertFile(files[fileIndex]);
}
private void convertFile(File f) {
if (f.getName().endsWith(".mp4")) {
String filename = f.getName().toLowerCase().toString();
System.out.println(filename);
File wavFile = new File(pathanme, filename);
IConvertCallback callback = new IConvertCallback() {
#Override
public void onSuccess(File convertedFile) {
Toast.makeText(NewMainActivity.this, "SUCCESS: " + convertedFile.getPath(), Toast.LENGTH_LONG).show();
fileIndex++;
if (this.files.size > fileIndex) {
convertFile(this.files[fileIndex];
} else {
// we're done converting
}
}
#Override
public void onFailure(Exception error) {
Toast.makeText(NewMainActivity.this, "ERROR: " + error.getMessage(), Toast.LENGTH_LONG).show();
// cancel out or keep going, whatever
}
};
Toast.makeText(this, "Converting audio file..." + filename, Toast.LENGTH_SHORT).show();
AndroidAudioConverter.with(this)
.setFile(wavFile)
.setFormat(AudioFormat.MP3)
.setCallback(callback)
.convert();
}
}
}
This question already has an answer here:
Android send mail with PDF file
(1 answer)
Closed 7 years ago.
I need send a PDF file attach on a message, I have a button that calls a function that open a Intent with message, email address and subject filled, but I need that the PDF file has been attached too.
This is my code and I can not find my error, someone can help me please?
public void initializeWebView() {
// Initialize the webview
webView.setResourceClient(new XWalkResourceClient(webView) {
#Override
public boolean shouldOverrideUrlLoading(XWalkView view, String stringUrl) {
if(stringUrl.equals(baseUrl)) {
return false;
}
// mailto links will be handled by the OS.
if (stringUrl.startsWith("mailto:")) {
Uri uri = Uri.parse(stringUrl);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
String fileName = "bouhnik.pdf";
String filePath = (Configuration.getMagazineAssetPath()).toString()+ File.separator + fileName;
Context c = getActivity().getApplicationContext();
File file = null;
FileOutputStream fos = null;
try {
InputStream is = c.getAssets().open(filePath);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
fos = new FileOutputStream(file);
fos.write(buffer);
fos.close();
} catch (IOException e) {
Log.i("Ferrou",e.toString());
e.printStackTrace();
}
if (!file.exists() || !file.canRead()) {
return false;
}
intent.putExtra(intent.EXTRA_STREAM, file.getPath());
intent.setClassName("com.android.email", "com.android.mail.compose.ComposeActivity");
intent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
WebViewFragment.this.startActivity(Intent.createChooser(intent, "Send email..."));
} else {
try {
URL url = new URL(stringUrl);
// We try to remove the referrer string to avoid passing it to the server in case the URL is an external link.
String referrer = "";
if (url.getQuery() != null) {
Map<String, String> variables = Configuration.splitUrlQueryString(url);
String finalQueryString = "";
for (Map.Entry<String, String> entry : variables.entrySet()) {
if (entry.getKey().equals("referrer")) {
referrer = entry.getValue();
} else {
finalQueryString += entry.getKey() + "=" + entry.getValue() + "&";
}
}
if (!finalQueryString.isEmpty()) {
finalQueryString = "?" + finalQueryString.substring(0, finalQueryString.length() - 1);
}
stringUrl = stringUrl.replace("?" + url.getQuery(), finalQueryString);
}
// Remove referrer from query string
if (!url.getProtocol().equals("file")) {
if (referrer.equals(WebViewFragment.this.getActivity().getString(R.string.url_external_referrer))) {
Uri uri = Uri.parse(stringUrl);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
WebViewFragment.this.startActivity(intent);
} else if (referrer.toLowerCase().equals(WebViewFragment.this.getActivity().getString(R.string.url_baker_referrer))) {
((IssueActivity) WebViewFragment.this.getActivity()).openLinkInModal(stringUrl);
return true;
} else {
return false;
}
} else {
stringUrl = url.getPath().substring(url.getPath().lastIndexOf("/") + 1);
int index = ((IssueActivity) WebViewFragment.this.getActivity()).getJsonBook().getContents().indexOf(stringUrl);
if (index != -1) {
Log.d(this.getClass().toString(), "Index to load: " + index + ", page: " + stringUrl);
((IssueActivity) WebViewFragment.this.getActivity()).getViewPager().setCurrentItem(index);
view.setVisibility(View.GONE);
} else {
// If the file DOES NOT exist, we won't load it.
File htmlFile = new File(url.getPath());
if (htmlFile.exists()) {
return false;
}
}
}
} catch (MalformedURLException | UnsupportedEncodingException ex) {
Log.d(">>>URL_DATA", ex.getMessage());
}
}
return true;
}
});
// Set UI Client (Start stop animations)
webView.setUIClient(new XWalkUIClient(webView) {
#Override
public void onPageLoadStopped(XWalkView view, String url, LoadStatus status) {
if(!url.isEmpty() && status == LoadStatus.FINISHED) {
if(isUserVisible) {
webView.resumeTimers();
}else{
webView.pauseTimers();
}
}
}
});
webView.load(baseUrl, null);
}
Thank's so much for everyone!!
I solve my problem change the type of Intent to:
Intent emailIntent = new Intent(Intent.ACTION_SEND);
Because this is better to email commands, and I define a emailUri where:
emailUri = Uri.fromFile(file.getAbsoluteFile());
because this get a absolute path with a file inside, and when the email client open, it open this file, not a path.
I add a type at my intent but I select the type of my attachment, so I define:
emailIntent.setType("application/pdf");
And finally:
emailIntent.putExtra(Intent.EXTRA_STREAM, uriMail);
startActivity(emailIntent);
It's works now!! Thanks :D
It looks like something is might be going wrong with your file path. Double check it. Then
1 - You need to add the package name of your application with context.getPackageName()
private String path = Environment.getExternalStorageDirectory().getPath() + context.getPackageName() + "books/"+fileName;
2 - Declare the permission inside your AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I have a JSF2 commandlink with an image. When the image is clicked, the server will download a PDF file. While the file is downloaded after the image link is clicked, it also causes the entire page to scroll to the top of the page. the code snippet for the link is as follows:
<p:commandLink ajax="false"
action="#{refereeAssessmentSummaryBean.stateLatestFormInPAVer(project.appId)}">
<p:graphicImage name="images/pdf.png"
title="#{msg['label.downloadpdf']}" />
</p:commandLink>
How can I use the commandlink to download the PDF file, without the webpage scrolling to the top of the page every time I click on it?
Edit: FWIW, added PDF download code. This code is called as a shared method from the backing bean. As you can see, the code will set the content type before streaming the PDF data to the client.
public void downloadEformPdf(Integer appId, Integer revNo, Integer meetingId,
String password, boolean showSaveDialog, boolean getEditedIfAvailable, boolean showVersionInfo) {
User user = WebUtils.getCurrentUser();
PermissionResult permissionResult = ServiceProxy.getPermissionService().checkViewOnlineProposalPermission(user, appId, meetingId);
if (permissionResult != PermissionResult.GRANTED) {
if (!(permissionResult == PermissionResult.REJECTED_GRBE_COI_NOT_APPROVED
|| permissionResult == PermissionResult.REJECTED_GRBE_COI_NOT_DECLARED)) {
throw new PermissionDeniedException("Permission Denied");
}
}
Application appl = ServiceProxy.getAppService().getApplication(appId);
String scheme = appl.getScheme();
boolean withNomination = false;
boolean isEditedVersion = false;
byte[] pdfData;
if (getEditedIfAvailable) {
if (revNo == null) {
Appmatching appMatching = ServiceProxy.getAppFormService().getLatestAppMatching(appId,false);
revNo = appMatching.getMainRevno();
}
Appattacheditedeform editedEntry = ServiceProxy.getAppService().getEditedProposalForApplication(appId, revNo, true);
// give GRB, ER the edited version if it exists
if (editedEntry != null) {
Filestorage storage = editedEntry.getFilestorage();
pdfData = storage.getContent();
isEditedVersion = true;
} else {
pdfData = ServiceProxy.getReportService().getHMRFReportContentByRevNo(
appId.intValue(), revNo, withNomination);
}
} else { //Get the unedited version
//Get latest rev no.
if (revNo == null) {
Appmatching appMatching = ServiceProxy.getAppFormService().getLatestAppMatching(appId,false);
revNo = appMatching.getMainRevno();
}
pdfData = ServiceProxy.getReportService().getHMRFReportContentByRevNo(
appId.intValue(), revNo, withNomination);
}
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext extContext = context.getExternalContext();
extContext.responseReset();
PDDocument doc = null;
try {
if (pdfData != null) {
PDFParser parser = new PDFParser(new ByteArrayInputStream(pdfData));
parser.parse();
doc = parser.getPDDocument();
AccessPermission ap = new AccessPermission();
ap.setReadOnly();
if (password != null) {
StandardProtectionPolicy spp = new StandardProtectionPolicy(password, password, ap);
spp.setEncryptionKeyLength(128);
doc.protect(spp);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
doc.save(bos);
doc.close();
byte[] docbuff = bos.toByteArray();
String refNo = appl.getRefNo();
String filename = null;
if (showVersionInfo) {
if (isEditedVersion) {
filename = scheme.toLowerCase() + "_eform_" + refNo + "_(v" + revNo + ")_(Edited).pdf";
} else {
filename = scheme.toLowerCase() + "_eform_" + refNo + "_(v" + revNo + ")_(PA).pdf";
}
} else {
filename = scheme.toLowerCase() + "_eform_" + refNo + ".pdf";
}
extContext.setResponseContentType("application/pdf");
extContext.setResponseContentLength(docbuff.length);
extContext.setResponseHeader("Content-Disposition", (!showSaveDialog) ? "inline"
: "attachment" + "; filename=\"" + filename + "\"");
OutputStream os = extContext.getResponseOutputStream();
os.write(docbuff);
os.close();
context.responseComplete();
} else {
extContext.setResponseContentType("text/html");
Writer writer = extContext.getResponseOutputWriter();
writer.write("Cannot retrieve PDF form for this proposal.");
writer.close();
context.responseComplete();
}
} catch (IOException e) {
logger.log(Level.ERROR, e.getMessage(), e);
} catch (COSVisitorException e) {
logger.log(Level.ERROR, e.getMessage(), e);
} catch (BadSecurityHandlerException e) {
logger.log(Level.ERROR, e.getMessage(), e);
} finally {
}
}
How do you generate the PDF?
Did you set a mimetype so that the brower will recognize that you respond with a pdf?
Did you also prevent primefaces from continuing the response after you have written your PDF file to it? (use facesContext.responseComplete(); for that)
When you use the default HTML link tag <a />, you have to set href='javascript:void(0)' to avoid the current page to scroll to the top.
Maybe there is a way with a p:commandLink to do the same thing
<p:commandLink url="javascript:void(0)" ... /> ??
Hope this will help you to resolve your problem
I think it's because you are using ajax=false.
If you are not using ajax the whole page will be reloaded.
Either remove it or change to ajax=true and give it a try.
Edit:
I was wrong. ajax=false is required when downloading files.