Sunday, August 6, 2017

android - Get raw resource uri

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    android:background="#f55572"
    >
    <Button
        android:id="@+id/btn_uri"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Raw Resource URI"
        />
    <TextView
        android:id="@+id/tv_stats"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:padding="10dp"
        />
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:scaleType="fitCenter"
        />
</LinearLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private LinearLayout mRootLayout;
    private Button mBtnGetURI;
    private TextView mTVStats;
    private ImageView mImageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from xml layout
        mRootLayout = findViewById(R.id.root_layout);
        mTVStats = findViewById(R.id.tv_stats);
        mBtnGetURI = findViewById(R.id.btn_uri);
        mImageView = findViewById(R.id.image_view);

        /*
            raw
                Arbitrary files to save in their raw form. To open these resources with a raw
                InputStream, call Resources.openRawResource() with the resource ID,
                which is R.raw.filename.
        */

        // Set a click listener for the button
        mBtnGetURI.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                    Uri
                        Immutable URI reference. A URI reference includes a URI and a fragment, the
                        component of the URI following a '#'. Builds and parses URI references
                        which conform to RFC 2396.
                */

                /*
                    Uri parse (String uriString)
                        Creates a Uri which parses the given encoded URI string.

                    Parameters
                        uriString String : an RFC 2396-compliant, encoded URI

                    Returns
                        Uri : Uri for this given uri string

                    Throws
                        NullPointerException : if uriString is null
                */

                // Get the uri of the image from raw folder
                Uri uri = Uri.parse(
                        "android.resource://"
                                + getPackageName()
                                + "/"
                                + R.raw.color_whele
                );

                // Display the raw resource uri in text view
                mTVStats.setText("URI of the image : \n"+uri.toString());

                // Set the image view image source as uri
                // Display the raw folder image in image view
                mImageView.setImageURI(uri);
            }
        });
    }
}