1

In the following code when I click on Button it will link to Gallery and pick image/video from it, but I want, Button to be redirect to filemanger and pick txt file from it. Please help to do so.

public class MainActivity extends Activity {
Button b1;
private static final int SELECT_PHOTO = 100;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1= (Button)findViewById(R.id.button1);
        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                photoPickerIntent.setType("image/*");
                //photoPickerIntent.setType("Document/*");
                //int SELECT_PHOTO;
                startActivityForResult(photoPickerIntent, SELECT_PHOTO);    
            }
        });

    }

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

        switch(requestCode) { 
        case SELECT_PHOTO:
            if(resultCode == RESULT_OK){  
                Uri selectedImage = imageReturnedIntent.getData();
              //  String[] filePathColumn = {MediaStore.Images.Media.DATA};
                String[] filePathColumn = {Environment.getExternalStorageDirectory().getAbsolutePath()};

                Cursor cursor = getContentResolver().query(
                                   selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                Intent i =new Intent(getApplication(), Activity2.class);
                i.putExtra("path",filePath );
                startActivity(i);
                Log.d("Here", filePath);
                cursor.close();
               // Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
                System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            }
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
Jenz
  • 8,280
  • 7
  • 44
  • 77
  • http://stackoverflow.com/questions/9923760/how-to-use-intent-for-choosing-file-browser-to-select-file try this way – RQube Apr 23 '14 at 05:38

1 Answers1

0

Use this code to intent.

if (Build.VERSION.SDK_INT < 19) {
        Intent intent = new Intent();
        intent.setType("image/jpeg");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Image."),
        GALLERY_INTENT_CALLED);
        } 
else {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/jpeg");
        startActivityForResult(intent,
        GALLERY_KITKAT_INTENT_CALLED);
        }

In your onActivityResult ;

public String getPath(Uri uri) {
        if( uri == null ) {
            return null;
        }
        String[] projection = { MediaStore.Images.Media.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
        if( cursor != null ){
            int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        return uri.getPath();
}


if (requestCode == GALLERY_INTENT_CALLED
                && resultCode == Activity.RESULT_OK) {
            originalUri = data.getData();
            selectedImagePath = getPath(originalUri);
            cropImageView.setImageBitmap(BitmapFactory.decodeFile(selectedImagePath));
            System.out.println("GALLERY_INTENT_CALLED : "
                    + originalUri.getPath());
        } else if (requestCode == GALLERY_KITKAT_INTENT_CALLED
                && resultCode == Activity.RESULT_OK) {
            originalUri = data.getData();
            ParcelFileDescriptor parcelFileDescriptor;
            try {
                parcelFileDescriptor = getActivity().getContentResolver().openFileDescriptor(originalUri, "r");
                FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
                parcelFileDescriptor.close();
                cropImageView.setImageBitmap(image);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
Ashwin S Ashok
  • 3,623
  • 2
  • 29
  • 36