2017-10-05 43 views
0

我在應用程序中工作,我希望用戶選擇多個並將它們轉換爲base64,以便我可以將它發送到服務器。它是不可能性,以從圖庫中選擇多張圖像,然後將它們轉換爲Base64,然後將其發送到服務器如何選擇多個圖像並將其轉換爲基本64?

  Intent intent = new Intent(); 
      intent.setType("*/*"); 


      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 
       intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 
      } 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(Intent.createChooser(intent, "android.intent.action.SEND_MULTIPLE"), SELECT_PICTURE); 



@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 

    if (requestCode == SELECT_PICTURE) { 

     if (resultCode == RESULT_OK) { 
      //data.getParcelableArrayExtra(name); 
      //If Single image selected then it will fetch from Gallery 
      filePath = data.getData(); 

       filePath = data.getData(); 
       if (null != filePath) { 
        try { 
         bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), filePath); 
         // img.setImageBitmap(bitmap); 
         if (filePath.getScheme().equals("content")) { 
          Cursor cursor = getContentResolver().query(filePath, null, null, null, null); 
          try { 
           if (cursor != null && cursor.moveToFirst()) { 
            file_name = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); 
            // text.setText(file_name+","); 
            img_name.add(file_name); 
            img_pic.add(getStringImage(bitmap)); 
            //  Toast.makeText(this, "1." + file_name, Toast.LENGTH_SHORT).show(); 
           } 
          } finally { 
           cursor.close(); 
          } 
         } else { 

          String path = data.getData().getPath(); 
          file_name = path.substring(path.lastIndexOf("/") + 1); 
          // text.setText(file_name); 
          img_name.add(file_name); 
          img_pic.add(getStringImage(bitmap)); 
          //Toast.makeText(this, "2." + file_name, Toast.LENGTH_SHORT).show(); 
         } 


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

的可能的複製(https://stackoverflow.com/questions/4830711 /如何對轉換-A-圖像 - 到 - 基於64位字符串) –

回答

0

當然,你可以管理自己的選擇和顯示圖像,也可以依靠機器人會文件意圖選擇器,讓他們選擇並返回。然後,您可以使用提供的URI來檢索圖像,轉換和發送。

獲取用戶選擇的圖像很簡單,所以我不會發布,但爲防萬一它是你不熟悉的東西,這裏是一個鏈接,將引導你通過它。 Select multiple images from android gallery

現在轉換爲Base64應的AsyncTask

使用以下:?如何將圖像轉換成字符串的Base64]

public class Base64EncodeMediaAsyncTask extends AsyncTask<Void, Void, MediaModel> { 

/*/////////////////////////////////////////////////////////////// 
// MEMBERS 
*//////////////////////////////////////////////////////////////// 
private static final String TAG = Globals.SEARCH_STRING + Base64EncodeMediaAsyncTask.class.getSimpleName(); 
private MediaModel mMediaModelToConvert; 


/*/////////////////////////////////////////////////////////////// 
// CONSTRUCTOR 
*//////////////////////////////////////////////////////////////// 
public Base64EncodeMediaAsyncTask(MediaModel model){ 
    mContext = context; 
    mMediaModelToConvert = model; //it's just a file wrapper, nothing special lol. 

} 


/*/////////////////////////////////////////////////////////////// 
// OVERRIDES 
*//////////////////////////////////////////////////////////////// 
@Override 
protected MediaModel doInBackground(Void... params) { 
    try{ 
     InputStream inputStream = new FileInputStream(mMediaModelToConvert.getAbsoluteLocalPath());//You can get an inputStream using any IO API 
     byte[] bytes; 
     byte[] buffer = new byte[(int) new File(mMediaModelToConvert.getAbsoluteLocalPath()).length()]; 
     int bytesRead; 

     ByteArrayOutputStream output = new ByteArrayOutputStream(); 
     while ((bytesRead = inputStream.read(buffer)) != -1) { 
      output.write(buffer, 0, bytesRead); 
     } 

     bytes = output.toByteArray(); 

     mMediaModelToConvert.setBase64String(Base64.encodeToString(bytes, Base64.DEFAULT)); 

    }catch (Exception ex){ 
     A35Log.e(TAG, "Failed to get base 64 encoding for file: " + mMediaModelToConvert.getAbsoluteLocalPath()); 
     return null; 

    } 

    return mMediaModelToConvert; 

} 
@Override 
protected void onPostExecute(MediaModel success) { 
    super.onPostExecute(success); 

} 

} 
相關問題