2014-03-07 74 views
1

我幾乎完成了我的應用程序,即時通訊上的菜單項工作現在, 的問題是設置一個位圖作爲牆紙和保存它從菜單項SD卡。位圖:在SD卡設置壁紙和保存在菜單項

我想的東西,第一個是創造它正在採取從以前的活動-again-同樣的數據的意圖,-I有失敗 -

用同樣的意圖第二-i已經失敗 -

這是我的代碼。

謝謝

public class Fullpic extends Activity { 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // menu 
     getMenuInflater().inflate(R.layout.menu_pics, menu); 

     return true; 
    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 

     switch (item.getItemId()) 
     { 

     case R.id.menu_set: 

      // setting as wallpaper 
      return true; 

     case R.id.menu_save: 

      // save the image on sdcard 

      return true; 


     case R.id.menu_back: 

      return true; 


     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 

    //menu end 


    // XML node keys 
    static final String KEY_THUMB_URL = "source"; 
    static final String HEIGHT_MAX = "600"; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_single_pic); 

     // getting intent data 
     Intent in = getIntent(); 

     // Get XML values from previous intent dude 
     String bid = in.getStringExtra(KEY_THUMB_URL); 
     int hoppa= Integer.parseInt(in.getStringExtra(HEIGHT_MAX)); 

     // Displaying all values on the screen 
     ImageView balls = (ImageView) findViewById(R.id.bals_label); 
     balls.getLayoutParams().height = hoppa-30; 



     URL url = null; 
     try { 
      url = new URL(bid); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     Bitmap bmp = null; 
     try { 
      bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     balls.setImageBitmap(bmp); 

    } 


    public Object fetch(String address) throws MalformedURLException, 
    IOException { 
     URL url = new URL(address); 
     Object content = url.getContent(); 
     return content; 
    } 
    private Drawable ImageOperations(Context ctx, String url) { 
     try { 
      InputStream is = (InputStream) this.fetch(url); 
      Drawable d = Drawable.createFromStream(is, "source"); 
      return d; 
     } catch (MalformedURLException e) { 
      return null; 
     } catch (IOException e) { 
      return null; 
     } 
    } 
} 
+0

我不知道是你有困難哪方面,如果只是下載您可以創建從InputStream您製作繪製對象時創建一個新的文件並將其保存到所需的位置後,將圖像保存。 – kabuto178

+0

@ kabuto178你能幫我嗎?我真的不能 – LikePod

+0

這是你想要做什麼? – kabuto178

回答

0
new Thread(new Runnable(){ 
     @Override 
     public void run(){ 
      String root = Environment.getExternalStorageDirectory().toString(); 
       File myDir = new File(root + "/blabla");  
       myDir.mkdirs(); 

       String fname = "name_here.jpg"; 
       File file = new File (myDir, fname); 
       if (file.exists()) 
        file.delete(); 
       try { 
         FileOutputStream out = new FileOutputStream(file); //create stream to output the contents of the image 
         bmp.compress(Bitmap.CompressFormat.JPEG, 90, out); //compress and write bitmap data to output file stream 
         out.flush(); 
         out.close(); //closes stream important to avoid memory leaks 

       } catch (Exception e) { 
         //exception handling 
       } 
      }}).start(); 

這會幫助你寫的位圖文件到您需要的是設備的SD卡上,並在一個線程中完成,以避開重行動主UI線程。乾杯。

+0

什麼是'在我們的代碼bmp' –