1

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

  1. Get the txt file from my res/raw resources
  2. 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

Amaury Medeiros
  • 2,093
  • 4
  • 26
  • 42

2 Answers2

2

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;
    }
}
deadfish
  • 11,996
  • 12
  • 87
  • 136
0

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

IvanF.
  • 513
  • 10
  • 18