Android save video in gallery from its path - java

I hope you all doing good. I have been trying to save video in my gallery. I have video path which is already saved in a hidden folder. I just want to save that video in my gallery. Here is the code I might making a mistake. If you can resolved it out I will be thankful to you.
File newfile;
AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(Uri.parse( path), "r");
FileInputStream in = videoAsset.createInputStream();
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
File dir = new File(root + "/" + "Pictures");
if (!dir.exists()) {
dir.mkdirs();
}
newfile = new File(dir, "status_"+System.currentTimeMillis()+".mp4");
if (newfile.exists()) newfile.delete();
OutputStream out = new FileOutputStream(newfile);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
By this code I am having this no content provider /storage/emulated/0/android/data/ exception. As I have already added provider for image and permissions to write storage, but I don't know what provider are needed for video's or the problem is with the code?

I have found the solution, the mistake I was making that not getting the proper FileInputStream from the start.
Just replace this
AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(Uri.parse( path), "r");
FileInputStream in = videoAsset.createInputStream();
with this and your good to go :D
FileInputStream in = new FileInputStream(new File(path));

Related

Java JPA retrieve blob from oracle database as its original file type

I am using java spring and jpa I have a query to retrieve the blob from an oracle db. I get the blob and if it is a text file it is downloading onto my local machine just fine - but if it is an image I have to go through a BufferedImage, and I am still working on PDF. So far I'm just seeing the extension of the file by getting it's original filename which is also stored in the DB, then filtering the string for the extension. When I get a PDF it says the file is corrupted or open in another window, which it is not.
So far, this is my code that tries to turn blob from DB into a file:
Blob blob = Service.retrieveBlob(ID);
if (blob == null){
return false;
}
String fileName = Service.getFileName(ID);
String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
System.out.println(extension);
if (extension.equals("jpg") || (extension.equals("png"))){
File file = new File("./DBPicture.png");
try(FileOutputStream outputStream = new FileOutputStream(file)) {
BufferedImage bufferedImage = ImageIO.read(blob.getBinaryStream());
ImageIO.write(bufferedImage, "png", outputStream);
System.out.println("Image file location: "+file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
}
else {
InputStream ins = blob.getBinaryStream();
byte[] buffer = new byte[ins.available()];
ins.read(buffer);
File targetFile = new File("./" + fileName);
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer);
}
available is simply that. it is not necessarily the length of the stream.
Try something like
InputStream ins = blob.getBinaryStream();
File targetFile = new File("./" + fileName);
OutputStream outStream = new FileOutputStream(targetFile);
byte[] buffer = new byte[8000];
int count = 0;
while ((count = ins.read(buffer)) != -1) {
outStream.write(buffer);
}
// then close your output
outStream.close ();

Converting ZipOutputStream to ZipInputStream

I am creating a ZipOutputStream and adding files to it this is being done in memory , later I want to be able to get/read files from this. I have tried multiple permutations and combinations but am not able to get it done.
I know if I use FileOutpuStream and attach a physical file to it I will be able to use it but I am not allowed to create physical file.
Current Code:-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(bos);
ZipEntry ze = new ZipEntry(“Temp.txt”);
zos.putNextEntry(ze);
FileInputStream fis = new FileInputStream("Test/File1.txt");
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) > 0) {
//System.out.println(new String(buffer));
zos.write(buffer, 0, len);
}
You're on the right track! To read the file back, simply use:
byte[] zipBytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(zipBytes);
ZipInputStream zis = new ZipInputStream(bis);

Incorrect upload file use FileOutputStream. Incorrect size

When I try upload the file from server, I use this code. I get the file size of 500kb, when the original file size about 300kb.
What am I doing wrong?
attachmentContent = applicationApi.getApplicationAttachmentContent(applicationame);
InputStream in = new BufferedInputStream(attachmentContent.getAttachmentContent().getInputStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n=0;
while (-1 !=(n=in.read(buf)))
{
out.write(buf,0,n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
File transferredFile = new File(attachmentName);
FileOutputStream outStream = new FileOutputStream(transferredFile);
attachmentContent.getAttachmentContent().writeTo(outStream);
outStream.write(response);
outStream.close();
Simplify. The same result:
File transferredFile = new File(attachmentName);
FileOutputStream outStream = new FileOutputStream(transferredFile);
attachmentContent.getAttachmentContent().writeTo(outStream);
outStream.close();
The ByteArrayOutputStream is a complete waste of time and space. You're reading the attachment twice, and writing it twice too. Just read from the attachment and write directly to the file. Simplify, simplify. You don't need 90% of this.

java download file does not work in file size is more than 8K?

i try to download a pdf file from url using the code below. It works fine when the file size is under 8k and if the file is up to 8k, it downloads the pdf file but the file is not readable.
Code
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream(new File("C:\\temp\\TEST.pdf"));
fos.write(response);
fos.close();
System.out.println("Download Finished");
You may try using the IOUtils.copy(InputStream input, OutputStream output)
You can minimize the lines of code.
Apache IOUtils

Opening PDF in browser taking huge time using java

I'm facing performance issue while opening pdf files in browser.It is taking very huge to time to open.Our pdf files are scanned images. So is there any alternative solution for this. Here is my sample code.
response.setContentType("application/pdf");
response.setContentLength((int) file.length());
FileInputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
Here i'm rendering outstream to browser.

Categories