如何从Android中的SD卡获取音频,视频,图像,文件的路径?

时间:2015-11-19 05:36:27

标签: android android-intent null getfiles

我有开发应用程序,从SD卡获取所有类型的路径(图像,音频,视频)...       它适用于打开默认Android浏览器(图库)。但是当我在其他应用程序中打开SD卡时,它会抛出NullPointer。例如,我通过我的应用程序打开SD卡使用资源管理器应用程序显示空指针异常...       我将保存图像路径到SQlite并从另一个活动中获取SQLite的内容。但是此Explorer应用程序显示所有类型,不仅适用于指定类型。 (示例i选择图像按钮,但它显示所有类型)。

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);
        }

2 个答案:

答案 0 :(得分:3)

试试这个:

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();
    }

答案 1 :(得分:0)

我们也可以在ContentProviders中使用:

        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();