2015-11-19 109 views
-1

我有開發的應用程序,從SDcard獲取所有類型(圖像,音頻,視頻)的路徑... 它可以正常打開默認Android瀏覽器(庫)。但是當我在其他應用程序中打開SD卡時,它會拋出NullPointer。例如,我已通過我的應用程序打開SDcard,使用Explorer應用程序顯示空指針異常... 我將保存圖像路徑到SQlite,並從SQLite獲取其他活動的內容。但是這個資源管理器應用程序顯示所有類型,不僅僅是指定的類(例如我選擇圖像按鈕,但它顯示所有類型)。如何從android中的SDcard獲取路徑爲音頻,視頻,圖像,文件?

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

'應用程序從SD卡獲取路徑'。你的代碼中沒有任何東西與SD卡有關。請說明如何在另一個應用程序中打開您的SD卡。 – greenapps

+0

'打開默認Android瀏覽器(圖庫)'。 Gallery應用程序不是瀏覽器,因此不是默認的Android瀏覽器。 – greenapps

+0

整體而言:你的文字和你的代碼之間幾乎沒有關係。 – greenapps

回答

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(); 
    } 
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(); 
相關問題