gpt4 book ai didi

android - 上传前调整图片大小(我上传的 take/pick 没问题)

转载 作者:太空狗 更新时间:2023-10-29 12:41:13 25 4
gpt4 key购买 nike

差不多两周后...我放弃了!!!

我实现了从图库和相机上传图片...

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!

但在使用...上传到服务器之前,我无法将图像的大小减小到 900px

- FileInputStream(sourceFile);  
- HttpURLConnection
- DataOutputStream( getOutputStream)
- dos.writeBytes(form... name... file name...)
- dos.write(buffer, 0, bufferSize)

我无法理解...
- 在这种情况下如何使用“createScaledBitmap”。
- 如果在创建新位图时它没有路径(至少我是这么认为的),我该如何使用“writeBytes(...filename=?)”。
- 如果我在磁盘上有原始图像,“createScaledBitmap”的结果路径是什么?
- 缓冲区如何工作(循序渐进会很棒),以及为什么在 stackoverflow 上的其他示例中不使用它?

我读过很多引用资料,包括:
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
但我已经使用“options.inSampleSize”在我的 fragment 上进行预览,我想我需要(在我的情况下)“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()

最佳答案

实际上有两种方法可以处理上述情况,如下所述:

1] 在服务器端(在您的网络服务中)进行一些安排,以便您可以在服务器上上传图像时传递高度和宽度,无论您传递的高度/宽度尺寸如何,这都会减小图像尺寸。这是第一个解决方案。

2] 根据我的理解,如果你可以通过这段代码减少位图尺寸:

try
{
int inWidth = 0;
int inHeight = 0;

InputStream in = new FileInputStream(pathOfInputImage);

// decode image size (decode metadata only, not the whole image)
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, options);
in.close();
in = null;

// save width and height
inWidth = options.outWidth;
inHeight = options.outHeight;

// decode full image pre-resized
in = new FileInputStream(pathOfInputImage);
options = new BitmapFactory.Options();
// calc rought re-size (this is no exact resize)
options.inSampleSize = Math.max(inWidth/dstWidth, inHeight/dstHeight);
// decode full image
Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options);

// calc exact destination size
Matrix m = new Matrix();
RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight());
RectF outRect = new RectF(0, 0, dstWidth, dstHeight);
m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER);
float[] values = new float[9];
m.getValues(values);

// resize bitmap
Bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap, (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true);

// save image
try
{
FileOutputStream out = new FileOutputStream(pathOfOutputImage);
resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
}
catch (Exception e)
{
Log.e("Image", e.getMessage(), e);
}
}
catch (IOException e)
{
Log.e("Image", e.getMessage(), e);
}

完成上述编码步骤后,您就可以使用您的图片/位图上传逻辑/代码了。

关于android - 上传前调整图片大小(我上传的 take/pick 没问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25396595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com