2012-05-09 1 views
0

안녕하세요, 저는 URL에서 설정 한 배경 화면에 게시 된 내용을 확인했습니다.하지만 프로그래밍에 익숙하지 않은데, 여전히 그것을 보완하지는 못합니다. 예를 들어, 기본적으로 예제를 제공 할 수 있습니까? 나는 서버에서 이미지를 가지고 있고이 버튼을 누르고 전화 배경 화면with url set up wallpaper from url 내가 이해할 수 없다.

public class TestingThree extends Activity { 

ImageView image; 

private class BackgroundTask extends AsyncTask 
<String, Void, Bitmap> { 
    protected Bitmap doInBackground(String...url) { 
     //--- download an image --- 
     Bitmap bitmap = DownloadImage(url[0]); 
     return bitmap; 

    } 
    protected void onPostExecute(Bitmap bitmap) { 
     ImageView image = (ImageView) findViewById(R.id.imageView1); 
     image.setImageBitmap(bitmap); 
    } 
} 

private InputStream OpenHttpConnection(String urlString) 
throws IOException 

{InputStream in = null; 
int response= -1; 

URL url = new URL(urlString); 
URLConnection conn = url.openConnection(); 


if (!(conn instanceof HttpURLConnection)) 
    throw new IOException("Not an HTTP connection"); 
try{ 
    HttpURLConnection httpConn = (HttpURLConnection) conn; 
    httpConn.setAllowUserInteraction(false); 
    httpConn.setInstanceFollowRedirects(true); 
    httpConn.setRequestMethod("GET"); 
    httpConn.connect(); 
    response = httpConn.getResponseCode(); 
    if (response == HttpURLConnection.HTTP_OK){ 
     in = httpConn.getInputStream(); 
    } 

    } 
catch (Exception ex) 
{ 
    throw new IOException("Error connecting"); 
} 
return in; 
} 


private Bitmap DownloadImage(String URL) 
{ 
    Bitmap bitmap = null; 
    InputStream in = null; 
    try { 
     in = OpenHttpConnection(URL); 
     bitmap = BitmapFactory.decodeStream(in); 
     in.close(); 

    } 
    catch (IOException e1){ 
     Toast.makeText(this,e1.getLocalizedMessage(), 
     Toast.LENGTH_LONG).show(); 

     e1.printStackTrace(); 

    } 
    return bitmap; 
} 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.wallpaper); 

    new BackgroundTask().execute("http://myglobaljournal.com/images/imagetest.jpg"); 

    Button setWallpaper = (Button) findViewById(R.id.button3); 
    setWallpaper.setOnClickListener(new OnClickListener() { 
     public void onClick(View view) { 
      WallpaperManager wManager; 
      Bitmap bitmap; 

      try { 
       bitmap = BitmapFactory.decodeFile(null); 
       wManager = WallpaperManager.getInstance(getApplicationContext()); 
       wManager.setBitmap(bitmap); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

    }); 
} 

} 

내가 이미지를 끌어 하단에있는 버튼을 구현하기 위해 노력하고 도움을 위해 다시 한번 감사로 설정하려면 링크에서 직접 및 배경 화면으로 설정 다시 고마워

+0

무엇 후, 당신은 이해하지 못했다 무엇을, 왜 당신은 언급하지 않았다? – Nanne

+0

시도한 코드를 게시 할 수 있습니까? – Bobbake4

+0

sory에 대해 내가 이해 못하는 코드 링크가 이것입니다. http://stackoverflow.com/questions/2205092/android-how-to-set-the-wallpaper-image – user1385487

답변

2

시도해보십시오 :

ImageView image; 
    private class BackgroundTask extends AsyncTask 
    <String, Void, Bitmap> { 
     protected Bitmap doInBackground(String...url) { 
      //--- download an image --- 
      Bitmap bitmap = DownloadImage(url[0]); 
      return bitmap; 
     } 
     protected void onPostExecute(Bitmap bitmap) { 
      ImageView image = (ImageView) findViewById(R.id.imageView1); 
      bitmaptwo=bitmap; 
      image.setImageBitmap(bitmap); 
     } 
    } 
    private InputStream OpenHttpConnection(String urlString) 
    throws IOException 
    {InputStream in = null; 
    int response= -1; 
    URL url = new URL(urlString); 
    URLConnection conn = url.openConnection(); 
    if (!(conn instanceof HttpURLConnection)) 
     throw new IOException("Not an HTTP connection"); 
    try{ 
     HttpURLConnection httpConn = (HttpURLConnection) conn; 
     httpConn.setAllowUserInteraction(false); 
     httpConn.setInstanceFollowRedirects(true); 
     httpConn.setRequestMethod("GET"); 
     httpConn.connect(); 
     response = httpConn.getResponseCode(); 
     if (response == HttpURLConnection.HTTP_OK){ 
      in = httpConn.getInputStream(); 
     } 
     } 
    catch (Exception ex) 
    { 
     throw new IOException("Error connecting"); 
    } 
    return in; 
    } 
    private Bitmap DownloadImage(String URL) 
    { 
     Bitmap bitmap = null; 
     InputStream in = null; 
     try { 
      in = OpenHttpConnection(URL); 
      bitmap = BitmapFactory.decodeStream(in); 
      in.close(); 
     } 
     catch (IOException e1){ 
      Toast.makeText(this,e1.getLocalizedMessage(), 
      Toast.LENGTH_LONG).show(); 

      e1.printStackTrace(); 
     } 
     return bitmap; 
    } 
    public Bitmap bitmaptwo; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.wallpaper); 
     new BackgroundTask().execute("http://myglobaljournal.com/images/imagetest.jpg"); 
     Button setWallpaper = (Button) findViewById(R.id.button3); 
     setWallpaper.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       WallpaperManager wManager; 

       try { 
        // bitmap = BitmapFactory.decodeFile(null); 
        wManager = WallpaperManager.getInstance(getApplicationContext()); 
        wManager.setBitmap(bitmaptwo); 

       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 

     }); 


    } 

필수 권한 : 코드가 어디에

<uses-permission android:name="android.permission.SET_WALLPAPER"/> 
+0

코드를 어디에 넣어야하는지 설명 할 수 있습니까? 매우 감사합니다. – user1385487

+0

bitmap = BitmapFactory.decodeFile (null); 코드에서이 줄 주석을 달고 onCreate 전에 비트 맵을 public static으로 만드십시오. –

+0

저는 예를 들어서 많은 것을 요구할 수 있습니다. – user1385487