0

How to pick a file from absolute path instead of gallery in Android

I am referring to this example for wifi direct

https://android.googlesource.com/platform/development/+/master/samples/WiFiDirectDemo/src/com/example/android/wifidirect/DeviceDetailFragment.java

The code to select a file from gallery is

  protected static final int CHOOSE_FILE_RESULT_CODE = 20;
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                            intent.setType("image/*");
                            startActivityForResult(intent, CHOOSE_FILE_RESULT_CODE);

Instead of this, I want to pick an image from absolute path like this

File f = new File(Environment.getExternalStorageDirectory() + "/test/testimage.jpg");

How can such an image be picked and submitted to intent in android?

Shajeel Afzal
  • 5,913
  • 6
  • 43
  • 70
1234567
  • 2,226
  • 4
  • 24
  • 69
  • Wha do you want to achieve by attaching it with intent? Do you want to send it to server or to second activity? Describe your scenario – Shajeel Afzal Feb 27 '16 at 11:28
  • Since you know the path of the image, you can directly convert it to the Bitmap and upload it to the server. Check this answer if you want to know how to convert it to Bitmap: http://stackoverflow.com/a/8710690/1773155 – Shajeel Afzal Feb 27 '16 at 11:35

2 Answers2

0

The code to select a file from gallery is

That code does not "select a file from gallery". That code requests a Uri to an image from any app with an activity that supports ACTION_GET_CONTENT. While gallery-type apps might support that Intent, so may other apps.

How can such an image be picked and submitted to intent in android?

There is nothing in Android itself for what you want. However, there are many file chooser libraries and image picker libraries for Android. One of those might meet your needs.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • ,you are correct i just want to upload an image from a desired path ,this very similar to uploading an image to server using sockets , can you provide any example or code for the same. or any proper example using wifidrect where we send image (without use of gallery) to a different phone – 1234567 Feb 27 '16 at 13:34
-1

You use this code work fine

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CHOOSE_FILE_RESULT_CODE  && resultCode == RESULT_OK && data != null && data.getData() != null) {

        Uri uri = data.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            // Log.d(TAG, String.valueOf(bitmap));

            ImageView imageView = (ImageView) findViewById(R.id.imageView);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

hope it helpuful

Rathod Vijay
  • 417
  • 2
  • 7