2013-05-29 3 views
51

기존 파일 경로에서 비트 맵 또는 드로어 블을 생성하려고합니다.파일 경로에서 비트 맵/드로어 블 만들기

String path = intent.getStringExtra("FilePath"); 
BitmapFactory.Options option = new BitmapFactory.Options(); 
option.inPreferredConfig = Bitmap.Config.ARGB_8888; 

mImg.setImageBitmap(BitmapFactory.decodeFile(path)); 
// mImg.setImageBitmap(BitmapFactory.decodeFile(path, option)); 
// mImg.setImageDrawable(Drawable.createFromPath(path)); 
mImg.setVisibility(View.VISIBLE); 
mText.setText(path); 

그러나 setImageBitmap(), setImageDrawable() 경로에서 이미지를 표시하지 않습니다. mText 경로를 인쇄했습니다. 다음과 같이 보입니다. /storage/sdcard0/DCIM/100LGDSC/CAM00001.jpg

무엇이 잘못 되었나요? 누구든지 나를 도울 수 있습니까?

+0

BitmapFactory.decodeFile (경로) : 여기 – toantran

+1

@ autobot_101은 디버그 모드에서'mBuffer'에'id'를 가지고 있습니다. 그러나 'mHeight','mWidth' 값은'-1'이고'mLayoutBounds'는'null'입니다. –

+0

그런 다음 파일 경로를 다시 확인해야합니다. 이는 이미지가 비트 맵 객체에 '부풀려 있지 않음'을 의미하기 때문입니다. 어쩌면 다른 이미지를 시도해 볼 수 있습니다 – toantran

답변

82

파일 경로에서 비트 맵을 만듭니다.

잘못된 파일 경로를 제공한다고 생각합니다. :) 희망이 도움이됩니다. ->이 당신을 위해 비트 맵 객체를 반환하지

Bitmap bitmap = BitmapFactory.decodeFile(filePath); 
+1

이미 저의 솔루션은 오래전에 나왔습니다.하지만이 문제는 OOM 오류도 처리 할 수 ​​있기 때문에 올바른 대답으로 받아 들일 것입니다. 큰 이미지를 다시로드합니다. 아주 깨끗하고 좋은 솔루션! 감사! –

+1

여기서 imageName은 문자열이라고 가정합니다. 아니면 어떤 특정 imageName입니까? – Jeet

+0

@JeetendraChoudhary 예 imageName은 이미지의 이름으로 최종 문자열이 될 수 있습니다. – CodeShadow

48

그것은 나를 위해 작동 :

File imgFile = new File("/sdcard/Images/test_image.jpg"); 
if(imgFile.exists()){ 
    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 
    //Drawable d = new BitmapDrawable(getResources(), myBitmap); 
    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest); 
    myImage.setImageBitmap(myBitmap); 

} 

편집 :

String sdcardPath = Environment.getExternalStorageDirectory().toString(); 
File imgFile = new File(sdcardPath); 
:

하드 코딩 된 sdcard에 디렉토리 위의 경우에 작동하지 않는 경우, 당신은 sdcard에 경로를 가져올 수 있습니다

+3

'Environment.getExternalStorageDirectory(). toString()'에서 SdCard 경로를 가져 와서 시도하십시오. – Antarix

0

경로를 통해 드로어 블에 액세스 할 수 없으므로 프로그래밍 방식으로 작성할 수있는 드로어 블과 함께 사람이 읽을 수있는 인터페이스가 필요한 경우.

는 클래스 어딘가에는 HashMap을 선언

이제
private static HashMap<String, Integer> images = null; 

//Then initialize it in your constructor: 

public myClass() { 
    if (images == null) { 
    images = new HashMap<String, Integer>(); 
    images.put("Human1Arm", R.drawable.human_one_arm); 
    // for all your images - don't worry, this is really fast and will only happen once 
    } 
} 

액세스를 위해 -

String drawable = "wrench"; 
// fill in this value however you want, but in the end you want Human1Arm etc 
// access is fast and easy: 
Bitmap wrench = BitmapFactory.decodeResource(getResources(), images.get(drawable)); 
canvas.drawColor(Color .BLACK); 
Log.d("OLOLOLO",Integer.toString(wrench.getHeight())); 
canvas.drawBitmap(wrench, left, top, null); 
3

을 음, 정적 Drawable.createFromPath(String pathName) 좀 더 간단 나에게 스스로를 디코딩보다는 보인다 사용 ... :-)

mImg이 단순한 ImageView 인 경우 필요하지 않으므로 mImg.setImageUri(Uri uri)을 직접 사용하십시오. . 당신이 부모의 높이로 비트 맵을 확장하고 Bitmap.createScaledBitmap 기능을 사용 선폭하려면

File sd = Environment.getExternalStorageDirectory(); 
File image = new File(sd+filePath, imageName); 
BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions); 
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true); 
imageView.setImageBitmap(bitmap); 

:

1
static ArrayList< Drawable> d; 
d = new ArrayList<Drawable>(); 
for(int i=0;i<MainActivity.FilePathStrings1.size();i++) { 
    myDrawable = Drawable.createFromPath(MainActivity.FilePathStrings1.get(i)); 
    d.add(myDrawable); 
} 
+3

답에 제공 한 코드를 설명해야합니다. –

18

해결책인가? 그것을 확인할 수 있습니까?
관련 문제