2010-02-01 5 views
13

일부 이미지 파일이 있으며,이 이미지 파일을 Uri로 가져오고 싶습니다. 내 코드에서는 이미지 파일의 경로와 파일 이름 만 알고 있습니다. 경로와 파일 이름에서 Uri를 어떻게 얻을 수 있습니까? I가 같은 질문을했다이미지 파일의 이름을 Uri로 가져올 수 있습니까?

Uri uri = Uri.fromFile(file); 

답변

42

, 당신은 항상 URI A를 변환 할 수 있습니다 내 파일 탐색기 활동 ...하지만 당신은 파일에 대한 contenturi 이미지, 오디오 및 비디오와 같은 mediastore 데이터를 지원 tht 알고 있어야합니다 .... 나는 sdcard에서 이미지를 선택하는 이미지 콘텐츠를 받고있어 당신을주고있다 .... 이 코드를 시도해보십시오 ... 당신을 위해 작동 할 수 있습니다 ...

public static Uri getImageContentUri(Context context, File imageFile) { 
     String filePath = imageFile.getAbsolutePath(); 
     Cursor cursor = context.getContentResolver().query(
       MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
       new String[] { MediaStore.Images.Media._ID }, 
       MediaStore.Images.Media.DATA + "=? ", 
       new String[] { filePath }, null); 
     if (cursor != null && cursor.moveToFirst()) { 
      int id = cursor.getInt(cursor 
        .getColumnIndex(MediaStore.MediaColumns._ID)); 
      Uri baseUri = Uri.parse("content://media/external/images/media"); 
      return Uri.withAppendedPath(baseUri, "" + id); 
     } else { 
      if (imageFile.exists()) { 
       ContentValues values = new ContentValues(); 
       values.put(MediaStore.Images.Media.DATA, filePath); 
       return context.getContentResolver().insert(
         MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
      } else { 
       return null; 
      } 
     } 
    } 
+0

감사합니다. 사실, URI가 아니라 Uri를 찾고 있습니다.하지만 거의 동일하므로 해결책을 찾을 수 있습니다. – skysign

+0

아, 나는 당신이 안드로이드 특정 Uri 클래스를 의미 몰랐다. 내 대답도 업데이트했습니다. –

+0

답장을 보내 주셔서 감사합니다. – skysign

2

: 당신은 안드로이드 열린 우리당 클래스를 사용하려는 경우,

File file = new File(path + File.pathSeparator + filename); 
URI uri = file.toURI(); 

을 또는 : 당신이 File이있는 경우

관련 문제