Saturday, June 22, 2013

Convert Uri return from Gallery to file path

The old exercise "Select Image using Android build-in Gallery" show how to selecte photo using Gallery app, in Uri form. In this exercise, the returned Uri is converted to file path in String form.

Convert Uri return from Gallery to file path

I also show using deprecated managedQuery() method for API under level 11, and CursorLoader() method for API level 11 or higher.

Layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold"
        android:layout_gravity="center_horizontal"
        android:autoLink="web" />
    
    <Button
        android:id="@+id/loadimage" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Load Image"/>
    <TextView 
        android:id="@+id/targeturi" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" />
    <TextView 
        android:id="@+id/targetpath1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" />
    <TextView 
        android:id="@+id/targetpath2" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" />
</LinearLayout>


MainActivity.java
package com.example.androidselectimage;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 TextView textTargetUri, textTargetPath1, textTargetPath2;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  Button buttonLoadImage = (Button)findViewById(R.id.loadimage);
        textTargetUri = (TextView)findViewById(R.id.targeturi);
        textTargetPath1 = (TextView)findViewById(R.id.targetpath1);
        textTargetPath2 = (TextView)findViewById(R.id.targetpath2);
        
        buttonLoadImage.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    Intent intent = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, 0);
   }});
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  super.onActivityResult(requestCode, resultCode, data);
  
  if (resultCode == RESULT_OK){
   Uri targetUri = data.getData();
   textTargetUri.setText("Uri: " + targetUri.toString());
   textTargetPath1.setText("path: " + getPathFromUri_managedQuery(targetUri));
   textTargetPath2.setText("path: " + getPathFromUri_CursorLoader(targetUri));
  }
 }
 
 //using deprecated managedQuery() method 
 private String getPathFromUri_managedQuery(Uri uri){
  String [] projection = {MediaStore.Images.Media.DATA};
  
        Cursor cursor = managedQuery( 
          uri, 
          projection,
          null,   //selection
          null,   //selectionArgs
          null   //sortOrder
        );
        
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();

        return cursor.getString(column_index);
 }

 //using CursorLoader() method for API level 11 or higher
 private String getPathFromUri_CursorLoader(Uri uri){
  
  String [] projection = {MediaStore.Images.Media.DATA};
  
  CursorLoader cursorLoader = new CursorLoader(
    getApplicationContext(),
    uri, 
          projection,
          null,   //selection
          null,   //selectionArgs
          null   //sortOrder
        );
  
  Cursor cursor = cursorLoader.loadInBackground();
  
  int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();

        return cursor.getString(column_index);    
 }
}


Note: To use CursorLoader() in your code, you have to modify AndroidManifest.xml to speciify android:minSdkVersion="11".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidselectimage"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidselectimage.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


download filesDownload the files.

No comments: