【发布时间】:2014-10-13 07:57:22
【问题描述】:
差不多两周后...我放弃了!!!
我实现了从图库和相机上传图片...
startActivityForResult... ok!
EXTERNAL_CONTENT_URI... ok!
ACTION_IMAGE_CAPTURE... ok!
onActivityResult... ok!
Activity.RESULT_OK ('cause I'm on Fragment)... ok!
getActivity().getContentResolver().query()... ok!
BitmapFactory.Options, opts.inSampleSize, .decodeFile... ok!
但是在上传到服务器之前,我无法将图像的大小减小到 900 像素...
- FileInputStream(sourceFile);
- HttpURLConnection
- DataOutputStream( getOutputStream)
- dos.writeBytes(form... name... file name...)
- dos.write(buffer, 0, bufferSize)
我听不懂...
- 在这种情况下如何使用“createScaledBitmap”。
- 如果我在创建新位图时没有路径(至少我认为是这样),我该如何使用“writeBytes(...filename=?)”。
- 如果我在磁盘上有一个原始图像,“createScaledBitmap”的结果路径是什么?
- 缓冲区是如何工作的(Step by Step 会很棒),为什么在 stackoverflow 的其他示例中不使用它?
我阅读了许多参考资料,包括:
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
但我已经使用“options.inSampleSize”在我的片段上进行预览,我认为我需要(在我的情况下)“createScaledBitmap”来实现我的 900x900px 图像进行上传。
如果有其他方法可以上传带有调整大小的图片包括...请告诉我!
(任何链接都会有所帮助)
我知道...我应该使用 AsyncTask...我正在努力! ;)
请考虑不要说那么技术性,因为我有最糟糕的学习 Android 组合:新手和说西班牙语! xD
添加:
有人可以帮忙解决@GVSharma 在这里所说的事情吗?
Upload compressed image
“你首先得到字符串路径na.so将它转换为位图并压缩它。而不是将字符串文件路径作为第一个参数发送给该方法,将第一个参数更改为位图位图。或者你只需要字符串文件路径然后再次转换将位图压缩为字符串。希望这个可以帮助你"
(我不知道该怎么做)
public int uploadFile(String sourceFileUri) {
final String fileName = sourceFileUri;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
...
} else {
try {
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);//This is just for info?
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\"; filename=\""+fileName+"\"" + lineEnd);//How put in here a resized bitmap?
dos.writeBytes(lineEnd);
//Here I'm lost!
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
//I think...
//this is a way to transfer the file in little pieces to server, right?, wrong?
//If anybody can explain this, step by step... THANKS!!!
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "Respuesta HTTP es: " + serverResponseMessage + ": " + serverResponseCode);
if(serverResponseCode == 200){
...
}
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
...
} catch (Exception e) {
...
}
dialog.dismiss();
return serverResponseCode;
} // End else block
}//End uploadFile()
【问题讨论】:
-
如果您只调整位图大小,请转到stackoverflow.com/questions/15759195/…
-
@DivyangMetalia 谢谢!这很有帮助!如果您知道我如何在帖子中添加该位图...请告诉我。
-
您可以将位图转换为Base64并上传。
-
@DivyangMetalia 可能不知道我在说什么,但我读到使用 BASE64 会导致某些设备内存不足......这是真的吗?
-
是的,当您的位图尺寸很大时,某些设备会抛出内存不足错误。但在这里你可以上传 900px 所以它上传。我认为您可以尝试使用 base64 进行图片上传。