2012-07-10 3 views
2

에 저장된 png 파일에서 비트 맵 만들기 SD 카드에 저장된 PNG 파일에서 비트 맵을 만든 다음 해당 비트 맵을 imageView에 설정하려고합니다. 하지만 작동하지 않습니다.sdcard (Android)

여기에 코드입니다 :

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 

import com.pxr.tutorial.xmltest.R; 

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Environment; 
import android.widget.ImageView; 

public class Getbackground { 
    URL url; 

    public static long downloadFile(URL url2) { 
    try { 

     URL url = new URL ("http://oranjelan.nl/oranjelan-bg.png"); 
     InputStream input = url.openStream();{ 
     try { 

      File fileOnSD=Environment.getExternalStorageDirectory();  
      String storagePath = fileOnSD.getAbsolutePath(); 
      OutputStream output = new FileOutputStream (storagePath + "/oranjelanbg.png"); 
       try { 

        byte[] buffer = new byte[1024]; 
        int bytesRead = 0; 
        while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) { 
        output.write(buffer, 0, bytesRead); 
        } 
        } finally { 
      output.flush(); 
      output.close(); 
//---------------------------------------------------------------------------------------------------------- 
      Bitmap BckGrnd = BitmapFactory.decodeFile(storagePath + "/oranjelanbg.png"); 
      ImageView BackGround=(ImageView)findViewById(R.id.imageView1); 
      BackGround.setImageBitmap(BckGrnd); 
//----------------------------------------------------------=----------------------------------------------- 
      } 
      } catch (IOException e) { 
      throw new RuntimeException(e); 
    } finally { 
     try { 
      input.close(); 
      } catch (IOException e) { 
      throw new RuntimeException(e); 
    } 
    }  
}  
     } catch (MalformedURLException ex) { 
     throw new RuntimeException(ex); 
    } catch (IOException e) { 
     throw new RuntimeException(e); 
    } 

     return 0; 
    } 
//----------------------------------------------------------------------------------------- 
    private static ImageView findViewById(int imageview1) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
//----------------------------------------------------------------------------------------- 
} 

[파일이 성공적으로 SD 카드에 부하를 수행하지만 내가보기에 IMG을받을 것으로 보인다 기운 다.

+0

흠은 문제를 해결하지 못하는 것 같습니다. –

답변

1

당신은 할 수 없습니다 ..

ImageView BackGround=(ImageView)findViewById(R.id.imageView1); 
BackGround.setImageBitmap(BckGrnd); 

어떻게 비 활동 클래스 GetbackgroundImageView의 참조를 얻을 수 있나요?

MainUI 스레드에서 UI 구성 요소를 업데이트 할 수 있고 비활성 클래스에있는 UI 구성 요소는 해당 호출 활동 클래스의 참조 (Context) 만 사용하여 업데이트 할 수 있습니다.

Getbackground가 완료된 후이 코드를 Activity 클래스에 넣으십시오.

+0

예! 고맙습니다! 그것의 일 prefectly 지금 !! –

+0

Welcome Buddy ..! 해피 코딩 ..! – user370305

+0

@ user370305 당신이 말했듯이 __How가 아닌 ​​액티비티 클래스 Getbackground에서 ImageView에 대한 참조를 얻을 수 있습니까? __ 그러나 컨텍스트와 이미지 뷰 참조를 'Getbackground' 생성자로 전달하면 가능합니다. 내가 맞습니까? 이미지를 lazyloading하기 위해, 우리는 보통 이것을 좋아합니다. – sunil

관련 문제