2011-05-25 60 views
2

來從SD卡上傳圖像我試圖將圖像發送到存儲在SD卡中的服務器。當我點擊一個按鈕時,它會打開SD卡並在網格視圖中顯示圖像。從那我想上傳我正在接觸它的圖像。我知道,將圖像上傳到服務器,但我不知道該如何選擇從SD卡中的圖片,請幫我.....如何通過在我的應用程序中挑選Android

以下是我的代碼....

public void library() 
{ 
    Intent myIntent = new Intent(); 
    myIntent.setType("image/*"); 
    myIntent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(myIntent,"Select Picture"), 101); 
} 
public void onActivityResult(int requestCode, int resultCode, Intent myIntent) 
{ 
    Intent data = null; 
    if (requestCode == 101 && data != null) 
    { 
    Uri selctedImageUri =data.getData(); 
    } 
    else 
    { 
     Toast toast = Toast.makeText(this, "No Image is selected.", Toast.LENGTH_LONG); 
    toast.show(); 
    } 
} 

如何繼續請幫我.....

回答

8

這是獲取圖庫圖片的意圖:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); 
    intent.setType("image/*"); 
    intent.putExtra("return-data", true); 
    startActivityForResult(intent, 1); 

那麼這段代碼是設置從圖庫中選擇圖像中的圖片瀏覽: 在活動結果上使用這個:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
     case 1: 
      if(requestCode == 1 && data != null && data.getData() != null){ 
       Uri _uri = data.getData(); 

       if (_uri != null) { 
        //User had pick an image. 
        Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null); 
        cursor.moveToFirst(); 

        //Link to the image 
        final String imageFilePath = cursor.getString(0); 
        Log.v("imageFilePath", imageFilePath); 
        File photos= new File(imageFilePath); 
        Bitmap b = decodeFile(photos); 
        b = Bitmap.createScaledBitmap(b,150, 150, true); 
        ImageView imageView = (ImageView) findViewById(R.id.select_image); 
        imageView.setImageBitmap(b); 
        cursor.close(); 
       } 
      } 
      super.onActivityResult(requestCode, resultCode, data); 
     } 
    } 

這種減少圖像大小的方法;

private Bitmap decodeFile(File f){ 
     try { 
      //decode image size 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

      //Find the correct scale value. It should be the power of 2. 
      final int REQUIRED_SIZE=70; 
      int width_tmp=o.outWidth, height_tmp=o.outHeight; 
      int scale=1; 
      while(true){ 
       if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) 
        break; 
       width_tmp/=2; 
       height_tmp/=2; 
       scale++; 
      } 

      //decode with inSampleSize 
      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize=scale; 
      return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
     } catch (FileNotFoundException e) {} 
     return null; 
    } 

如果您傳遞一個包含圖像將調整圖像的大小,並返回位圖文件中的這些方法..

+1

感謝名單Mr.Loser烏爾示例代碼給了我,我的問題的想法... – 2011-05-26 04:47:09

相關問題