2011-12-30 30 views
1

//下面如何從SD卡中選擇圖像文件,然後如何將其發送到服務器?

在我的應用我有一個按鈕從SD卡選擇圖像並單擊它,我想探索所有的圖像,然後用戶將在此之後,選擇的圖像文件將選擇任何圖像被上傳到服務器。

而且我還有另一個按鈕從相機點擊它,首先我想拍快照,然後上傳到服務器,請通過示例任何方式告訴我??

+2

[請在發帖之前做一些調查](http://www.google.com.pk/search?q=android+how+to+upload+picture+to+server) – 2011-12-30 06:08:17

+0

@ ravi-我以某種方式試圖編輯你的問題,但它仍然是一個神祕的。 – 2011-12-30 06:10:39

回答

0

這是很容易通過deafult畫廊使用 意圖所以通過使用意圖,你可以輕鬆地選擇從SD卡 並從您的文件瀏覽器的圖像瀏覽圖像。

1

這是我用來查詢手機的MediaStore並返回包含所有圖像的遊標對象的代碼。之後,您可以將它們上傳到您的服務器,但我建議您在AsyncTask中處理第一部分。

class LoadImagesFromSDCard extends AsyncTask<Object, LoadedImage, Object> { 

    //Load images from SDCARD and display 
    @Override 
    protected Object doInBackground(Object... params) { 
     //setProgressBarIndeterminateVisibility(true); 
     Bitmap bitmap = null; 
     Bitmap newBitmap = null; 
     Uri uri = null;    

     // Set up an array of the Thumbnail Image ID column we want 
     String[] projection = {MediaStore.Images.Thumbnails._ID}; 
     // Create the cursor pointing to the SDCard 
     Cursor cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
       projection, // Which columns to return 
       null,  // Return all rows 
       null,  
       null); 
     int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); 
     int size = cursor.getCount(); 
     // If size is 0, there are no images on the SD Card. 
     if (size == 0) { 
      //No Images available, post some message to the user 
     } 
     int imageID = 0; 
     for (int i = 0; i < size; i++) { 
      cursor.moveToPosition(i); 
      imageID = cursor.getInt(columnIndex); 
      uri = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID); 
      try { 
       bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); 
       if (bitmap != null) { 
        newBitmap = Bitmap.createScaledBitmap(bitmap, 70, 70, true); 
        bitmap.recycle(); 
        if (newBitmap != null) { 
         publishProgress(new LoadedImage(newBitmap)); 
        } 
       } 
      } catch (IOException e) { 
       //Error fetching image, try to recover 
      } 
     } 
     cursor.close(); 
     return null; 
    } 
相關問題