Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why and how to use the @xml/file_paths resource in Android Studio?

tl;dr: Why do I need a file_paths resource, when I retrieve a file_path dynamically during runtime?

I am currently learning Android Studio by building my own app. What I want to do right now is to take a picture and then get some Text out of it. To take a picture and use it lateron, I followed the Taking Photos Simply guide of the Android Developers. At this point, it is suggested to use a File Provider.

In the manifest, the File Provider is added like this:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.mydomain.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

I am struggling with the meta-data, especially the android:resource=@xml/file_paths"

In the Taking Photos Simply Tutorial, this is the required addition to the XML to make it work:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" />
</paths>

So my question is: What is the good practise intended within the developer guide that I should follow, and how do I do that?

Edit: To make things clearer, I am having trouble understanding how to use a resource here at all. This is the method code straigh out of the guide:

private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }

So I am dynamically getting the name of the file (and therefore it's file path) during the Runtime of my application. What is that file/paths for, I do not understand its usage.

like image 305
InDaPond Avatar asked Feb 03 '18 14:02

InDaPond


People also ask

Why do we use XML file in Android?

XML stands for eXtensible Markup Language, which is a way of describing data using a text-based document. Because XML is extensible and very flexible, it's used for many different things, including defining the UI layout of Android apps.

How does XML work in Android Studio?

XML tags define the data and used to store and organize data. It's easily scalable and simple to develop. In Android, the XML is used to implement UI-related data, and it's a lightweight markup language that doesn't make layout heavy. XML only contains tags, while implementing they need to be just invoked.

Is XML used in Android Studio?

Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts. You can also use Android Studio's Layout Editor to build your XML layout using a drag-and-drop interface.

How do I use FileProvider on Android?

To make FileProvider work follow these three steps: Define the FileProvider in your AndroidManifest file. Create an XML file that contains all paths that the FileProvider will share with other applications. Bundle a valid URI in the Intent and activate it.


1 Answers

Create a folder in the res directory and name it xml, create a file file_paths in it and replace everything with this:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="my_images" path="Pictures" />
</paths>

if you want to preview the captured image from that tutorial then decode the file from its path to bitmap and then display it onto a ImageView from the onActivityResult()

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        val bitmap=BitmapFactory.decodeFile(currentPhotoPath)
        imageView?.setImageBitmap(bitmap)
    }
}
like image 87
Tarish Avatar answered Sep 17 '22 03:09

Tarish