void imageUpload(String urlString, String filename)
{
String fileURL = "/sdcard/baedaltong/baedaltong.png";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try {
FileInputStream mFileInputStream = new FileInputStream(fileURL);
URL connectUrl = new URL(urlString);
Log.d("Test", "mFileInputStream is " + mFileInputStream);
// open connection
HttpURLConnection conn = (HttpURLConnection)connectUrl.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
// write data
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + filename+"\"" + lineEnd);
dos.writeBytes(lineEnd);
int bytesAvailable = mFileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
int bytesRead = mFileInputStream.read(buffer, 0, bufferSize);
Log.d("Test", "image byte is " + bytesRead);
// read image
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = mFileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = mFileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
Log.e("Test" , "File is written");
mFileInputStream.close();
dos.flush(); // finish upload...
// get response
int ch;
InputStream is = conn.getInputStream();
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 ){
b.append( (char)ch );
}
String s=b.toString();
Log.e("Test", "result = " + s);
dos.close();
} catch (Exception e) {
Log.d("Test", "exception " + e.getMessage());
// TODO: handle exception
}
}
위의 소스를 이용해서 서버오 이미지전송을 이용하고있습니다.
제가 궁금한건 위 소스는 이미지를 sdcard에서 FileInputStream으로 불러와서 그것을 전송하는데
이렇게가 아니라
@Override
public void onActivityResult( int requestCode, int resultCode, Intent data ) {
if( requestCode == 0 ) {
if(data != null) {
try {
AssetFileDescriptor afd = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap selPhoto = BitmapFactory.decodeFileDescriptor(afd.getFileDescriptor(), null, options);
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
}
catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
}
위 소스처럼 갤러리에서 불러온 이미지를 비트맵으로 변환후 이 비트맵을 바로 전송하는 방법이 궁금합니다.
비트맵을 파일스트림으로 변환하는방법이있나요?
어떻게 해야할까요?