Insert Non media (pdf, txt ,doc) android - android

my application will downloads non media files like PDF,TXT, XML etc. Using following code I am saving in storage.
FileOutputStream out = new FileOutputStream("Save locaation");
final int fileBufferSizee = 1024;
byte[] buffer = new byte[fileBufferSizee ];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
working fine. Actually I need URI of saved file. for that I need to insert information in android media database.
How to insert non media files in android media database ? any suggestions ?

if u want to get URL from absolute path then do this
Uri.parse(new File("/sdcard/cats.jpg").toString()));
save this in one URI variable

Related

Android I saved a photo in removable storage using SAF, but Gallery don't recognize it and I cannot open it

I finally figure out how to save a photo in an arbitrary location in removable storage in Android by using a storage access framework.
However, I encounter another problem.
I successfully saved a photo in a path like /storage/6265-6530/DCIM/Camera but I cannot see the photo in Gallery. Moreover, I try to open the photo through the file browser but I cannot open it.
I connect the phone to the PC and I can open the photo I saved in PC.
So how to fix it?
below is my code:
DocumentFile pickedDir = DocumentFile.fromTreeUri(reactContext, mUri);
DocumentFile file = pickedDir.createFile("image", "myPhoto.jpg");
InputStream in = new FileInputStream(path);
OutputStream out = getContentResolver().openOutputStream(file.getUri());
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) { //read is successful
out.write(buffer, 0, read);
}
in.close();
out.flush();
out.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,file.getUri()));
I have called sendBroadcast to scan the file but still, the gallery didn't show my photo.

Creating a File object from a resource

I have a file located at /res/introduced.xml. I know that I can access it in two ways:
1) the R.introduced resource
2) some absolute/relative URI
I'm trying to create a File object in order to pass it to a particular class. How do I do that?
This is what I ended up doing:
try{
InputStream inputStream = getResources().openRawResource(R.raw.some_file);
File tempFile = File.createTempFile("pre", "suf");
copyFile(inputStream, new FileOutputStream(tempFile));
// Now some_file is tempFile .. do what you like
} catch (IOException e) {
throw new RuntimeException("Can't create temp file ", e);
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
some absolute/relative URI
Very few things in Android support that.
I'm trying to create a File object in order to pass it to a particular class. How do I do that?
You don't. A resource does not exist as a file on the filesystem of the Android device. Modify the class to not require a file, but instead take the resource ID, or an XmlResourceParser.

mp3 not saving to sd correctly; how to save mp3 to sd card?

I've been looking at this site for the past 3 or so hours. How to copy files from 'assets' folder to sdcard?
This is the best I could come up with because I'm only trying to copy one file at a time.
InputStream in = null;
OutputStream out = null;
public void copyAssets() {
try {
in = getAssets().open("aabbccdd.mp3");
File outFile = new File(root.getAbsolutePath() + "/testf0lder");
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (IOException e) {
Log.e("tag", "Failed to copy asset file: ", e);
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
I've figured out how to create a file and save a text file. http://eagle.phys.utk.edu/guidry/android/writeSD.html
I would rather save an mp3 file to the sdcard rather than a text file.
When I use this code I provided, I get a text document that same size as the aabbccdd.mp3 file. It does not create a folder and save an .mp3 file. It saves a text document in the root folder. When you open it, I see a whole bunch of chinese letters, but at the top in English I can see the words WireTap. WireTap Pro was the program I used to record the sound so I know the .mp3 is passing through. It's just not creating a folder and then saving a file like the above .edu example.
What should I do?
I think you should do something like that -[Note: this i used for some other formats not mp3 but its works on my app for multiple format so i hope it will work for u too.]
InputStream in = this.getAssets().open("tmp.mp3"); //give path as per ur app
byte[] data = getByteData(in);
Make sure u have the folder already exists on path, if folder is not there it will not save content correctly.
byteArrayToFile(data , "testfolder/tmp.mp3"); //as per ur sdcard path, modify it.
Now the methods ::
1) getByteData from inputstream -
private byte[] getByteData(InputStream is)
{
byte[] buffer= new byte[1024]; /* or some other number */
int numRead;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try{
while((numRead = is.read(buffer)) > 0) {
bytes.write(buffer, 0, numRead);
}
return bytes.toByteArray();
}
catch(Exception e)
{ e.printStackTrace(); }
return new byte[0];
}
2) byteArrayToFile
public void byteArrayToFile(byte[] byteArray, String outFilePath){
FileOutputStream fos;
try {
fos = new FileOutputStream(outFilePath);
fos.write(byteArray);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

Store remote PDF to Android Internal Storage

In my app I want to store a remote pdf to internal storage, so that no other application will have access to it. Can anyone please help.
Thanks in advance.
My code to store pdf file is :
File newdir = new File(getFilesDir().getAbsolutePath(),"/n.pdf");
newdir.mkdirs();
try {
FileOutputStream fos = new FileOutputStream(newdir);
URL url = new URL("my_pdf_path");
urlConnection = url.openConnection();
urlConnection.connect();
InputStream input = url.openStream();
byte[] buffer = new byte[1024];
int read;
while ((read = input.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
fos.close();
input.close();
} catch (Exception e) {
}
And when I try to access the above path, it says : "Error opening file. It does not exist or cannot be read".
You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them. See http://developer.android.com/guide/topics/data/data-storage.html#filesInternal for full details. Read the answer to this question for a java example of how to download the file from the web. Android - downloading image from web, saving to internal memory in location private to app, displaying for list item

How can I set my android app background from a server?

I'm quite new to android programming and I have the following problem.
I want to be able to put an image om my server and then if I use my app it should use that image as a background.
From previous research I understand I cant save any files to the drawable file?
So is this even possible?
I am now this far:
URL url = new URL ("http://oranjelan.nl/oranjelan-bg.png");
InputStream input = url.openStream();
try {
String storagePath = Environment.getExternalStorageDirectory();
OutputStream output = new FileOutputStream (storagePath + "/oranjelangb.png");
try {
byte[] buffer = new byte[1000000];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} finally {
input.close();
}
But I get the following error
# String storagePath = Environment.getExternalStorageDirectory();
The compiller says cannot convert file to string.
It should be possible. Simple steps may include :-
1) Download image file from server, Store it to SDcard or assets folder.
links for step 1 >> link1 link2
2) Create a Bitmap from the file you downloaded.
3) Set that bitmap as a Background image.
You can pick steps and search on SO there should be lots of answers available.

Categories

Resources