1
I have a problem that I haven’t been able to solve for a few days. I have a list of files that are saved in Internal Storage and what you need to do is zip those files when users leave the activity. The problem is that when I finally call the method to do such a thing it simply cannot find the files even with the correct path! Briefly the way I use to create and recover these files is to create a openFileOutput()
and then get the reference with new File(getFileDir(), "nome do arquivo")
.
Methods
private void zip(List<File> files, File zipFile) {
try {
byte[] buffer = new byte[1024];
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
for (int i = 0; i < files.size(); i++) {
File f = files.get(i);
FileInputStream fis = new FileInputStream(f);
zos.putNextEntry(new ZipEntry(f.getName()));
int len;
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
zos.closeEntry();
fis.close();
}
fos.close();
zos.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
public boolean build(List<File> files, File zipFile) {
zip(files, zipFile);
/* Eu sei que esse retorno não faz tanto sentido */
return true;
}
Activity
public class BotActivity extends AppCompatActivity {
/* ... */
@Override
protected void onPause() {
super.onPause();
startJob();
}
/* Aqui é onde eu obtenho o arquivo e salvo na lista
onde posteriormente será zipada */
private void talk(String text) {
request = new SynthesizeSpeechPresignRequest();
request.withVoiceId(VoiceId.Vitoria);
request.withOutputFormat(OutputFormat.Ogg_vorbis);
request.withText(text);
Needle.onBackgroundThread().execute(() -> {
URL pssu = client.getPresignedSynthesizeSpeechUrl(request);
try {
byte[] data = new byte[1024];
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
InputStream stream = pssu.openStream();
int len;
while ((len = stream.read(data)) != -1) {
byteBuffer.write(len);
}
String fileName = text.concat(".ogg");
FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
fos.write(byteBuffer.toByteArray());
fos.close();
audios.add(new File(getFilesDir(), fileName));
byteBuffer.close();
stream.close();
mediaPlayer.setDataSource(pssu.toString());
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
});
}
private void startJob() {
File zipFile = new File(getFilesDir(), SPFFile.SPF_FILE_NAME);
if (spfFile.build(audios, zipFile)) {
GooglePlayDriver googlePlayDriver = new GooglePlayDriver(this);
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(googlePlayDriver);
Bundle extras = new Bundle();
extras.putString(Constant.INTENT_JOB_KEY, data.getKey());
Job uploadJob = dispatcher.newJobBuilder()
.setService(UploadJob.class)
.setExtras(extras)
.setTag("UploadJob")
.setRecurring(false)
.setTrigger(Trigger.executionWindow(0, 0))
.setReplaceCurrent(true)
.setLifetime(Lifetime.FOREVER)
.setConstraints(Constraint.ON_ANY_NETWORK)
.build();
dispatcher.mustSchedule(uploadJob);
}
}
}
Stacktrace
I’m sorry for the censorship, but it’s because the code is from an unpublished product. But I guarantee that they do not hinder the understanding of the situation.
PS: I ran the same code with Intellij IDEA to see how the code worked on the desktop and everything went as expected.
From what you can see in the image, in the error part, it seems that the file name has a space between 1 and the point: "file 1 . ogg". I think that space is too much.
– ramaral
Hello @ramaral, in the image there is a space, but was guilty of the censorship I did. In the original text there is no such space between the file name and the extension. But in the name itself there is yes.
– Ivan Silva