Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get Exact path of a photo saved in Mobile Gallery?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 416
    Comment on it

    1)Suppose we have a Button and a TextView in XML:

    activity_main.xml
    
    <Button
        android:id="@+id/btnGet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GET IMAGES" />
    <TextView
        android:id="@+id/tvOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnGet"
        android:textSize="20dp" />
    


    2)In your relative Java file,onCreate method will be:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnGet = (Button) findViewById(R.id.btnGet);
        listView = (ListView) findViewById(R.id.listView);
        tvOne = (TextView) findViewById(R.id.tvOne);
        btnGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                    Toast.makeText(MainActivity.this, "EXTRA MULTIPLE ", Toast.LENGTH_LONG).show();
                }
                try {
                    startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), REQUEST_PICK_FILE);
                } catch (android.content.ActivityNotFoundException ex) {
                    ex.printStackTrace();
                }
            }
        });
    

    Here as u can see we have startActivityForResult(Intent,RequestCode).We will override a method named as onActivityResult().


    3)We will override a method here:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == REQUEST_PICK_FILE) {
            if (resultCode == RESULT_OK) {
                Uri uri = intent.getData();
                String imgPath = getPath(uri, this);
                Log.i("path is....", imgPath);
                tvOne.setText(imgPath);
            }
        }
    }
    

    Here we have called a function getPath(uri,this),which will return Exact Path and we will save it in imgPath of type String.

    4)Here we will define a function getPath(uri,this):

    public static String getPath(Uri uri, Context context) {
        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    

    That's all.Hope it helps:)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: