2017-11-12 5 views
0

이것은 훨씬 쉬워야합니다 ...하지만 URI에서 이미지의 전체 경로를 가져 오려고합니다.MediaStore가 NULL을 반환 함

String[] projection = { MediaStore.MediaColumns.DATA}; 

또는 ...

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

이 ... 나만 NULL 줄 것이다 :

String[] projection = { MediaStore.MediaColumns.DISPLAY_NAME}; 

올바르게 파일의 이름을 표시하지만 것입니다.

어쩌면 나는 너무 피곤하고 똑바로 생각하지 않을 수 있습니다 (이것은 훨씬 더 큰 응용 프로그램의 아주 작은 부분입니다).하지만 이미지가 분명히있을 경우 어떻게 가능할 지 이해할 수 없습니다. 비트 맵은 잘 작동하고 표시 이름이 작동합니다. 나는 쉬운 것을 놓치고 있다고 확신한다 ... 나는 지금 막 볼 수 없다.

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

    if (requestCode == 1 && resultCode == Activity.RESULT_OK) { 

     Uri selectedImageUri = null; 
     if (data != null) { 

      selectedImageUri = data.getData(); 
      String[] projection = { MediaStore.MediaColumns.DISPLAY_NAME}; 

      ContentResolver cr = this.getApplicationContext().getContentResolver(); 

      Cursor metaCursor = cr.query(selectedImageUri, 
        projection, null, null, null); 
      metaCursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); 

      if (metaCursor != null) { 
       try { 
        if (metaCursor.moveToFirst()) { 
         imagepath = metaCursor.getString(0); 
         Log.i(TAG, "UriB: " + metaCursor.getString(0)); 
        } 
       } finally { 
        metaCursor.close(); 
       } 
      } 

      Log.i(TAG, "Uri: " + selectedImageUri.toString()); 

     } 





     try { 
      Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImageUri)); 
      imageview.setImageBitmap(bitmap); 
      messageText.setText("Uploading file path: " + imagepath); 
     } catch (IOException ie) { 
      messageText.setText("Error"); 
     } 


    } 
} 

의 AndroidManifest.xml 내가 지금 열려 그 중 약 7을 가지고 있기 때문에 많은 페이지가이 거기에 알고

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.jon.bon" > 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE_EXTERNAL_STORAGE"/> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" > 


     <activity android:name=".MainActivity" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".DisplayMessageActivity" > 
     </activity> 
    </application> 

하지만, I :

는 전체 코드는 다음과 같습니다 왜 내가 갇혀 있는지 알 수가 없어. 내가 EXTERNAL_CONTENT_URI을 사용해야 할 수도 있습니다처럼

편집

것 같습니다. 나는이 할 때 :

Cursor metaCursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null); 

을 ... 내가 상관없이 내가 함께 작업하려고 어떤 이미지 일부 (실제로 ONE 매우 구체적인 ... 음) 완전히 임의 경로를 가져올 수 없습니다. 그것은 전혀 관련이없는 것처럼.

+1

안드로이드 사용 권한을 처리 했습니까? – muazhud

+0

@muazhud 예. 외부 읽기 및 쓰기. (내가 매니 페스트에서 다른 것을 망쳐 버렸을 수도 있습니다. 내 게시물을 업데이트했습니다. –

+0

os 버전을 사용하고있는 버전은 –

답변

0

아래의 사용 권한 코드는 'Intent.ACTION_PICK'에서 작동합니다.

Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       startActivityForResult(intent, Constants.PICK_GALLERY); 

그렇다면 장치 사양을 추가하십시오. OnActivity 결과는 다음과 같습니다.

if(responseCode == activity.RESULT_OK && requestCode==Constants.PICK_GALLERY){ 
     Uri selectedImage = data.getData(); 
     String[] filePath = {MediaStore.Images.Media.DATA}; 
     try { 
      Cursor c = activity.getContentResolver().query(selectedImage, filePath, null, null, null); 
      c.moveToFirst(); 
      int columnIndex = c.getColumnIndex(filePath[0]); 
      String picturePath = c.getString(columnIndex); 
      c.close(); 
      if(picturePath==null) { 
       picturePath=selectedImage.getPath(); 
      } 
      //Use picturePath for setting bitmap or to crop 
     }catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
+0

의도 상 "심볼 상수를 해결할 수 없습니다"오류가 발생했습니다 –

+0

Constants.PICK_GALLERY는 요청 코드입니다. 대신에 양의 정수를 사용할 수 있습니다 .. – ADM

+0

와우 ... 코스. 정말 피곤하셔야합니다! 하하! 20 시간 만에이 프로젝트를 진행하고 있습니다. 적어도 2 명이이 프로젝트를 진행하고 있습니다. 우승자가 있습니다! 많은 감사드립니다. –

관련 문제