2011-12-22 3 views
26

안드로이드의 drawable 폴더에서 이미지 경로를 String으로 얻을 수있는 방법이 있습니까? 나는 자신의 viewflow를 구현하기 때문에 SD 카드에서 자신의 경로를 사용하여 이미지를 보여주고 있기 때문에 필요하다. 그러나 보여줄 이미지가 없을 때 기본 이미지를 보여주고 싶다.안드로이드는 문자열로 drawable에서 이미지 경로를 얻습니다.

아이디어가 있습니까?

+1

내가 [여기]를 읽으면서

[수

String imageUrl = getURLForResource(R.drawable.personIcon); // Load image Glide.with(patientProfileImageView.getContext()) .load(imageUrl) .into(patientProfileImageView); 

로딩 이미지의

getURLForResource(R.drawable.personIcon) 

완료 예 1] 파일이 apk 안에 있기 때문에 불가능합니다. [1] : http://stackoverflow.com/questions/6301493/android-get-path-of-resource –

+0

이 정답이다. http://stackoverflow.com/a/25758286/7795876 – Hangman

답변

-6

이렇게하면 drawable 폴더의 특정 이미지로 이동할 수 있습니다. 자료를 얻을 수있다, 다음 액세스 원하는 이미지를 지정

Resources res = getResources(); 
Drawable drawable = res.getDrawable(R.drawable.myimage); 

자세한 내용은 Drawable resources를 참조하십시오.

+0

질문에 대답하지 않습니다. drawable 폴더에서 이미지를 얻는 방법을 알고있는 것처럼 보이지만 R.drawable.myimage는 int를 반환하고 Resource 폴더의 문자열 경로를 찾습니다. – lagos

-5

먼저 파일이 SDCard에 있는지 확인하십시오.

String imageUri = "drawable://" + R.drawable.image; 

다른 방법 : 파일 doesnot이 SD 카드에 존재하는 경우에 당신은이 모두 방법 당김 폴더

샘플 코드

File imageFile = new File(absolutepathOfImage);//absolutepathOfImage is absolute path of image including its name 
     if(!imageFile.exists()){//file doesnot exist in SDCard 

     imageview.setImageResource(R.drawable.defaultImage);//set default image from drawable folder 
     } 
34

에서 이미지 setImageResource를 (사용) methodand 통과 기본 이미지를 설정할 수 있습니다 내가 테스트 한 것

Uri path = Uri.parse("android.resource://com.segf4ult.test/" + R.drawable.icon); 
Uri otherPath = Uri.parse("android.resource://com.segf4ult.test/drawable/icon"); 

String path = path.toString(); 
String path = otherPath .toString(); 
+0

모든 파일 형식이 지원되지 않는 오류가 발생합니다. – NarendraJi

+1

그런 패턴은 FileNotFound 예외를 반환합니다 –

+0

첫 번째 접근 방식이 작동하지 않습니다. – mshah

0

이미지를 얻으려는 경우 경로를 사용하는 경우 드로어 블 폴더의 경로를 파악하는 대신 애셋을 사용하는 것이 좋습니다. 상기 응답의 일부에 기초

InputStream stream = getAssets().open("image.png"); 
    Drawable d = Drawable.createFromStream(stream, null); 
2

난 조금

이 방법을 생성하고 자원

재사용 방법

public String getURLForResource (int resourceId) { 
    return Uri.parse("android.resource://"+R.class.getPackage().getName()+"/" +resourceId).toString(); 
} 

샘플을 통과하여 통화를 즉석 전화

당신이 백분율 파일에 함수 getURLForResource 이동과 정적 때문에 재사용 할 수
관련 문제