-1

I have developing app which get the path from SDcard for all types (Images, Audio, Video)... It works fine for opening the Default Android Browsers(Gallery). But when i open the SDcard in other app's it throws NullPointer. For example i have open the SDcard through my app using Explorer app shows null pointer exception... I will save the image path to SQlite and get the content from the SQLite on another activity. But this Explorer app shows all types, not only for specified type. (Example i select image button, but it shows all types).

radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup rd_group, int checked_id) {

             checking = null;

            switch(checked_id) {
               case R.id.radio_image:
                   checking = "image";
                   break;



               case R.id.radio_audio:
                   checking = "audio";
                   break;


               case R.id.radio_video:
                   checking = "video";
                   break;
                        }

                    }

                }); 
         // I have a radio button for type of content will shown only


select_content.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        if(checking.equals("")) {

                            Toast.makeText(getApplicationContext(), "Please select the content type", Toast.LENGTH_SHORT).show();

                            return;

                                } 



   // when i select image, show image content only in SDcard. 

   // When i select audio, show only audio contents.

     // when i select video, show only video contents.




                        if(checking.equals("image")) {

                               Intent intent_image = new Intent();

                               intent_image.setType("image/*");
                               intent_image.setAction(Intent.ACTION_GET_CONTENT);
                               startActivityForResult(Intent.createChooser(intent_image, "Select Image"), SELECT_IMAGE_DIALOG);
                                    }
                        if(checking.equals("audio")) {

                               Intent intent_audio = new Intent();

                               intent_audio.setType("audio/*");
                               intent_audio.setAction(Intent.ACTION_GET_CONTENT);
                               startActivityForResult(Intent.createChooser(intent_audio, "Select Audio"), SELECT_AUDIO_DIALOG); 
                                    }   


                        if(checking.equals("video")) {

                               Intent intent_video = new Intent();

                               intent_video.setType("video/*");
                               intent_video.setAction(Intent.ACTION_GET_CONTENT);
                               startActivityForResult(Intent.createChooser(intent_video, "Select Video"), SELECT_VIDEO_DIALOG);
                                    }
                        }


                });




public void onActivityResult(int requestCode, int resultCode, Intent result) {
 //             imagePath = null;
    if (requestCode == SELECT_IMAGE_DIALOG) {

        if (resultCode == Activity.RESULT_OK) {

            Uri data = result.getData();

            Log.d("DATA", data.toString());

            selected_Path = getPath(data);

            final_path.getBytes();

            selected_path_text.setText(final_path);

            Log.d("Image Path", final_path);

                    }
                }



        if (requestCode == SELECT_AUDIO_DIALOG) {

            if (resultCode == RESULT_OK) {

            Uri data = result.getData();

            Log.d("DATA", data.toString());

            selected_Path = getPath(data);

            final_path.getBytes();

            selected_path_text.setText(final_path);

            Log.d("Audio Path", final_path);
                  }
                }



        if (requestCode == SELECT_VIDEO_DIALOG) {
            if (resultCode == RESULT_OK) {

            Uri data = result.getData();

            Log.d("DATA", data.toString());

            selected_Path = getPath(data);

            final_path.getBytes();

            selected_path_text.setText(final_path);

            Log.d("Video Path", final_path);
                    }


                }
        }




private String getPath(Uri data) {

    String[] projection = { MediaStore.Images.Media.DATA };

    Cursor cursor = managedQuery(data, projection, null, null, null);

    column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

    cursor.moveToFirst();

     final_path = cursor.getString(column_index);

     Log.d("Image Path", final_path);

    return cursor.getString(column_index);
        }
curiousMind
  • 2,812
  • 1
  • 17
  • 38
Kabil Dev
  • 1
  • 2
  • 'app which get the path from SDcard'. There is nothing in your code that does something with an SD card. Please show how you open your sd card in another app. – greenapps Nov 19 '15 at 07:03
  • 'opening the Default Android Browsers(Gallery).'. The Gallery app is not a browser and hence not a default android browser. – greenapps Nov 19 '15 at 07:05
  • For the whole: There is little relation between your words and your code. – greenapps Nov 19 '15 at 07:08
  • @greenapps. I want to open my SDcard for selecting Images (if i checked image radio button) or Selecting Videos(if i checked video radio button) or Selecting audio (if i checked audio radio button). The problem is when i checked image radio button and open card means createChooser opens without File Explorers what are i installed in my mobile. The Gallery has shows the exact type i have checked in my radio button. But in other apps (Like Explorer) they show another type of files also(i checked audio, those apps show video and images also)... Please send me the solution.. – Kabil Dev Nov 21 '15 at 13:14
  • Please put all info in your post. Not in a comment. Then remove most of your code. Just keep the code to pick a file of one type. Solve one problem first. Your buttons and checkboxes are of no interest. Only the way you use an Intent. – greenapps Nov 21 '15 at 14:25

2 Answers2

3

Try this:

Intent i = new Intent(Intent.android.provider.MediaStore.{File type        for getting path }.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, RESULT_CODE);





 if (requestCode == RESULT_CODE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.{FILETYPE  }.Media.DATA};
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        path = cursor.getString(columnIndex);
        cursor.close();
    }
santoXme
  • 802
  • 5
  • 20
0

We can do by usin ContentProviders also:

        private Cursor mCursor;
        if (audio) {

            mUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

            mCursor = mContentResolver.query(mUri, null, null, null, null);

            if (mCursor != null && mCursor.moveToFirst()) {

                do {

                        mFileName = mCursor
                                .getString(mCursor
                                        .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));

                        mFilePath = mCursor
                                .getString(mCursor
                                        .getColumnIndex(MediaStore.Audio.Media.DATA));
                    } while (mCursor.moveToNext());
            }

        }

        if (video) {

            mUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

            mCursor = mContentResolver.query(mUri, null, null, null, null);

            if (mCursor != null && mCursor.moveToFirst()) {

                do {

                        mFileName = mCursor
                                .getString(mCursor
                                        .getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME));

                        mFilePath = mCursor
                                .getString(mCursor
                                        .getColumnIndex(MediaStore.Video.Media.DATA));
                    } while (mCursor.moveToNext());
            }

        }

        if (image) {

            mUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

            mCursor = mContentResolver.query(mUri, null, null, null, null);

            if (mCursor != null && mCursor.moveToFirst()) {

                do {

                        mFileName = mCursor
                                .getString(mCursor
                                        .getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));

                        mFilePath = mCursor
                                .getString(mCursor
                                        .getColumnIndex(MediaStore.Images.Media.DATA));
                    } while (mCursor.moveToNext());
            }
        }

    mCursor.close();
Yasoda
  • 538
  • 5
  • 6