I have a .txt file inside my raw folder. I want to get the uri of the file and create a new file to upload to firebase.
The idea is simple
Get the txt file from my res/raw resources
Upload it to firebase storage
I have done this
String path = "android.resource://" + getPackageName() + "/" + "raw/grupos.txt";
Uri file = Uri.fromFile(new File(path));
but when I call
riversRef.putFile(file)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {....
it says that the file is invalid
LOGCAT
java.io.FileNotFoundException:
/android.resource:/com.example.usuario.ottasubirarchivos/raw/grupos.txt:
open failed: ENOENT (No such file or directory)
I don't know if I need to get the path of the resource file, create a new file inside the internal storage, get that file and upload it to firebase, or either use stream... I don't know how to handle this... Thanks
You could use this:
public class UriUtils {
public static Uri getUriToResource(#NonNull Context context, #AnyRes int resId) throws Resources.NotFoundException {
Resources res = context.getResources();
Uri resUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
"://" + res.getResourcePackageName(resId)
+ '/' + res.getResourceTypeName(resId)
+ '/' + res.getResourceEntryName(resId));
return resUri;
}
}
Do you have this working?
I can access a raw resource this way: Android.Net.Uri.Parse("android.resource://" + ApplicationContext.PackageName + "/" + Resource.Raw.beep)
With the file beep.ogg located under Resources/raw directory. What I've noticed here are: the capital letter in Raw, Resource and not ResourceS and the extensionless filename in my path. As noted by CommonsWare in comments you don't have a file but its Uri so parse it directly
Related
I am trying to check the mp3 file located to my raw folder under my res. my mp3 file is exist but my code gives me a false return.
String filename = "android.resource://" + this.getPackageName() + "/raw/w";
Toast.makeText(ViewPager.this, mp3check(filename)+"",Toast.LENGTH_LONG).show();
checking the mp3 file
public boolean mp3check(String _filename) {
return new File(_filename).exists();
}
I don't think you can treat a raw resource as a regular file.
Since it is a resource, there is no need to check if it exists.
You can open it using the resource ID.
For example, if your file is named, mysong.mp3, you can open it like this:
InputStream is = getResources().openRawResource(R.raw.mysong);
or
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.mysong);
You can use the AssetFileDescriptor to play it with a MediaPlayer.
try this
String filename = "android.resource://" + this.getPackageName() + "/raw/w";
String newFilePath = "new file path"; // ExternalStorageDirectory path
final Path destination = Paths.get(newFilePath );
try (
final InputStream in = getResources().openRawResource(R.raw.w);
) {
Files.copy(in, destination);
}
Toast.makeText(ViewPager.this, mp3check(filename)+"",Toast.LENGTH_LONG).show();
public boolean mp3check(String _newFilePath) {
return new File(_newFilePath).exists();
}
I'm working on an app that saves thermal images from a FLIR camera to the SD Card on the phone.
I'm using Android Marshmallow and I have to use the FLIR SDK.
In the FLIR SDK is a class "Frame". The class has a method "Frame.save", that needs a Java.io.File to save a thermal image: This is from the documentation:
public void save(java.io.File file,
RenderedImage.Palette previewPalette,
RenderedImage.ImageType previewImageType)
throws java.io.IOException,
java.lang.IllegalArgumentException
Saves a thermal JPEG file, which has a rendered visual preview and embedded thermal data
When I understand it right, on KitKat or higher I have to use the Storage Accsess Framework. So I used it. Send a Intent and get back a Uri to a picked folder on the SD Card. Now to the tricky part. This funktion should create a File and return a Java.io.File that ready for the "Frame.save" method.
public File getJavaFile (String Name, Uri myUri) {
DocumentFile pickedDir = DocumentFile.fromTreeUri(context, myUri); // Document file aus URI
DocumentFile DocumentFile = pickedDir.createFile("image/plain", Name + ".jpg" );
File file = new File(DocumentFile.getUri().getPath());
if(file.canWrite()){
Log.d(TAG + "/getJavaFile", "File can write");
}else {
Log.e(TAG + "/getJavaFile", "File cannot write");
}
Log.d(TAG + "/getJavaFile:", "File Created:" + file.getPath());
return file;
}
The file that the function return is not readable or writeable...
Otherwise, when I try to create a File directly, like so:
String root = Environment.getExternalStorageDirectory().toString();
File file = new File(root + "/saved_images");
Log.d(TAG, file.getPath());
Then the the system always gives me this emulated folder:
E/MainActivity: /storage/emulated/0/saved_images
So the question is: How to get a useable Java File, thats stored on the public storage space... Thank you for your time!
I am downloading a pdf file from server and saving it on sd card without extension (for security purpose so that normal user can't open that file from file manager).For e.g.- I am downloading abc.pdf and saving it on sd card with abc on below path
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "files");
And later I want to view that file by adding extension.
So when I am accessing that file from code by using below code then its gives error file does't exist..
String s=Environment.getExternalStorageDirectory() + "/files/" + "abc";
File pdfFile = new File(s+".pdf");
So how can I save file in sd card without extension and later open that file with extension.
Workaround for your problem is to rename your saved pdf file without extension to filename with .pdf extension & after completing/accessing view or read operation on your new pdf extension file again you can rename it to file without extension
Example Usage:
String currentFileName = Environment.getExternalStorageDirectory() + File.separator
+ "files" + File.separator + "abc";
String renamedFilename = currentFileName + ".pdf";
boolean isRenamed = renameFile(currentFileName, renamedFilename);
Log.d("isRenamed: ", "" + isRenamed);
if(isRenamed {
//perform your operation
}
Again rename the file if not in use (here exchange renameFile() parameters):
boolean renameAfterOperation = renameFile(renamedFilename, currentFileName);
Log.d("renameAfterOperation : ", "" + renameAfterOperation );
And here is renameFile(currentFilePath, renamedFilePath):
public boolean renameFile(String currentFilePath, String renamedFilePath){
File currentFile = new File(currentFilePath);
File newFile = new File(renamedFilePath);
boolean isrenamed = currentFile.renameTo(newFile);
return isrenamed;
}
In this way whenever you want to perform operation on saved file first rename it to .pdf & whenever it's not in use, again rename it without extension. Let me know if this works for you..
When I call this :
File directory = new File("file:///android_asset/");
then this :
directory.isDirectory()
Always returns false.
Am I doing something wrong here ?
(Sorry if it is a dumb question)
Edit:
I store several images in the assets folder and I wanted to load them in my app (like an image gallery).
Call
directory.mkdirs();
after you created the File object
Edit:
in your case try to change file url as below:
new File("/mnt/external_sd/");
or find the true path by looking it in eclipse File explorer.. do not start it with file://
May be you are searching for this:
String fullPath = "/data/data/" + this.getPackageName() + "/" + path;
File dir = new File(fullPath);
if (!dir.exists())
dir.mkdir();
i would download with DownloadManager a file in a specific directory. At the beginning of my app, i create (if doesn't exist) a folder:
if (storageState.equals(Environment.MEDIA_MOUNTED)) {
externalStorageWriteable = true;
directory_path = Environment.getExternalStorageDirectory()
+ "/" + context.getResources().getString(R.string.app_name);
directory = new File(directory_path);
if (!directory.exists()) {
directory.mkdirs();
}
}
if i check with a filemanager, folder it's available at /sdcard/folderName but if i print on logcat directory_path variable, i obtain
/storage/emulated/0/folderName
I try to download file by passing destination folder like
Uri destination = Uri.fromFile(new File(directory_path));
request.setDestinationUri(destination);
//request is of type DownloadManager.Request
but when i check where file it's stored, my folder is empty and downloaded file is stored inside /sdcard/Download
Could someone tell me why and how can i solve this issue?
Add .toString()
Environment.getExternalStorageDirectory().toString()