2012-07-30 38 views
1

我希望用戶上傳個人資料圖片(通過相機/圖庫),然後將其顯示爲用戶在應用程序上的個人資料照片。 (這是一個社交應用程序)。也希望它被保存在緩衝存儲器上,因爲當用戶使用應用程序時,應用程序不必下載它。使用http POST(php)上傳圖像並將路徑存儲在mySql上,如何在將活動保存在緩存中時將其顯示在活動上?

問1.如何將圖片上傳到網絡服務器?用http post方法並將圖像路徑存儲在sql上。問:2.如何顯示應用程序上使用的活動圖像。問3.如何將圖像保存在緩存中?

請諮詢我或幫我編碼,如果你可以請。

非常感謝你。

+0

右側的任何鏈接都有幫助嗎? – 2012-07-30 04:24:15

+0

可以幫助Q1 n Q2但不能與Q3 – Loshi 2012-07-30 04:25:00

+0

你指的是什麼緩存? – 2012-07-30 04:26:17

回答

0

@Loshi這裏是我如何上傳音頻到我的服務器位於Webhost.com。張你需要上傳圖片,希望這有助於。我在這裏做的是將我的文件保存到SD卡,然後在1次單擊上傳到服務器。

public class Audiofileload extends Activity 
{ 


private static final String LOG_TAG = "AudioRecordTest"; 
private static String mFileName = null; 

private RecordButton mRecordButton = null; 
private MediaRecorder mRecorder = null; 

private PlayButton mPlayButton = null; 
private MediaPlayer mPlayer = null; 
private Upload Upload = null; 


private void onRecord(boolean start) { 
    if (start) { 
     startRecording(); 
    } else { 
     stopRecording(); 
    } 
} 

private void onPlay(boolean start) { 
    if (start) { 
     startPlaying(); 
    } else { 
     stopPlaying(); 
    } 
} 

private void startPlaying() { 
    mPlayer = new MediaPlayer(); 
    try { 
     mPlayer.setDataSource(mFileName); 
     mPlayer.prepare(); 
     mPlayer.start(); 
    } catch (IOException e) { 
     Log.e(LOG_TAG, "prepare() failed"); 
    } 
} 

private void stopPlaying() { 
    mPlayer.release(); 
    mPlayer = null; 
} 

private void startRecording() { 
    mRecorder = new MediaRecorder(); 
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    mRecorder.setOutputFile(mFileName); 
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 



    try { 
     mRecorder.prepare(); 
    } catch (IOException e) { 
     Log.e(LOG_TAG, "prepare() failed"); 
    } 

    mRecorder.start(); 
} 

private void stopRecording() { 
    mRecorder.stop(); 
    mRecorder.release(); 
    mRecorder = null; 
} 


class Upload extends Button { 
    boolean Upload = true; 

    OnClickListener clicker = new OnClickListener() { 
     public void onClick(View v) { 
      HttpURLConnection conn = null; 
      DataOutputStream dos = null; 
      DataInputStream inStream = null; 
      String existingFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/testin.mp4"; 
      String lineEnd = "\r\n"; 
      String twoHyphens = "--"; 
      String boundary = "*****"; 
      int bytesRead, bytesAvailable, bufferSize; 
      byte[] buffer; 
      int maxBufferSize = 1*1024*1024; 
      String responseFromServer = ""; 
      String urlString = "http://sirisak.comule.com/11409.php"; 
      try 
      { 
       CLIENT REQUEST 
      FileInputStream fileInputStream = new FileInputStream(new File(existingFileName)); 
      // open a URL connection to the Servlet 
      URL url = new URL(urlString); 
      // Open a HTTP connection to the URL 
      conn = (HttpURLConnection) url.openConnection(); 
      // Allow Inputs 
      conn.setDoInput(true); 
      // Allow Outputs 
      conn.setDoOutput(true); 
      // Don't use a cached copy. 
      conn.setUseCaches(false); 
      // Use a post method. 
      conn.setRequestMethod("POST"); 
      conn.setRequestProperty("Connection", "Keep-Alive"); 
      conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 
      dos = new DataOutputStream(conn.getOutputStream()); 
      dos.writeBytes(twoHyphens + boundary + lineEnd); 
      dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd); 
      dos.writeBytes(lineEnd); 
      // create a buffer of maximum size 
      bytesAvailable = fileInputStream.available(); 
      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      buffer = new byte[bufferSize]; 
      // read file and write it into form... 
      bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
      while (bytesRead > 0) 
      { 
       dos.write(buffer, 0, bufferSize); 
       bytesAvailable = fileInputStream.available(); 
       bufferSize = Math.min(bytesAvailable, maxBufferSize); 
       bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
      } 
      // send multipart form data necesssary after file data... 
      dos.writeBytes(lineEnd); 
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
      // close streams 
      Log.e("Debug","File is written"); 
      fileInputStream.close(); 
      dos.flush(); 
      dos.close(); 
      } 
      catch (MalformedURLException ex) 
      { 
       Log.e("Debug", "error: " + ex.getMessage(), ex); 
      } 
      catch (IOException ioe) 
      { 
       Log.e("Debug", "error: " + ioe.getMessage(), ioe); 
      } 
      read the SERVER RESPONSE 
      try { 
        inStream = new DataInputStream (conn.getInputStream()); 
        String str; 

        while ((str = inStream.readLine()) != null) 
        { 
         Log.e("Debug","Server Response "+str); 
        } 
        inStream.close(); 

      } 
      catch (IOException ioex){ 
       Log.e("Debug", "error: " + ioex.getMessage(), ioex); 
      } 
     } 
    }; 

    public Upload(Context ctx) { 
     super(ctx); 
     setText("Upload"); 
     setOnClickListener(clicker); 
    } 
} 





class RecordButton extends Button { 
    boolean mStartRecording = true; 

    OnClickListener clicker = new OnClickListener() { 
     public void onClick(View v) { 
      onRecord(mStartRecording); 
      if (mStartRecording) { 
       setText("Stop recording"); 
      } else { 
       setText("Start recording"); 
      } 
      mStartRecording = !mStartRecording; 
     } 
    }; 

    public RecordButton(Context ctx) { 
     super(ctx); 
     setText("Start recording"); 
     setOnClickListener(clicker); 
    } 
} 



class PlayButton extends Button { 
    boolean mStartPlaying = true; 

    OnClickListener clicker = new OnClickListener() { 
     public void onClick(View v) { 
      onPlay(mStartPlaying); 
      if (mStartPlaying) { 

       setText("Stop playing"); 
      } else { 
       setText("Start playing"); 
      } 
      mStartPlaying = !mStartPlaying; 
     } 
    }; 

    public PlayButton(Context ctx) { 
     super(ctx); 
     setText("Start playing"); 
     setOnClickListener(clicker); 
    } 
} 

public Audiofileload() { 
    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath(); 
    mFileName += "/testin.mp4"; 
} 

public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 

    LinearLayout ll = new LinearLayout(this); 

    mRecordButton = new RecordButton(this); 
    ll.addView(mRecordButton, 
     new LinearLayout.LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT, 
      0)); 


    mPlayButton = new PlayButton(this); 
    ll.addView(mPlayButton, 
     new LinearLayout.LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT, 
      0)); 

    Upload = new Upload(this); 
    ll.addView(Upload, 
     new LinearLayout.LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT, 
      0)); 
    ll.setBackgroundResource(R.drawable.kasiriss); 

    setContentView(ll); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    if (mRecorder != null) { 
     mRecorder.release(); 
     mRecorder = null; 
    } 

    if (mPlayer != null) { 
     mPlayer.release(); 
     mPlayer = null; 
    } 
} 
} 
+0

ohh好..所以你把它保存在SD卡上b4發送http帖子ri8 ... kool thnx – Loshi 2012-07-30 04:45:06

+0

你明白了,如果回答你的問題不要忘記讓讓社區知道。 – KaSiris 2012-07-30 04:47:56

+0

如果您只是將代碼轉儲到您的項目中,您可以通過訪問http://sirisak.comule.com/11409/testing.mp4來測試它以聽到音頻。這就是如果有人需要幫助。 – KaSiris 2012-07-30 04:52:21

相關問題