- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
差不多两周后...我放弃了!!!
我实现了从图库和相机上传图片...
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/
请看下面我的构造函数,我正在从字符串创建一个分数: public Fraction(String str) { if(str.isEmpty()) {
当我向不可变 map 添加对象时,我注意到了这一点。 如果我添加一个对象,例如: Map model = ImmutableMap.of( "post", post
偶尔在我的网站上,我注意到无法加载外部文件。如果我重命名它并指向新名称,它就可以正常工作。旧名称不再有效。 通常这发生在图像上,但今天它发生在 css 文件上。它会自然而然地发生,而且是不可预测的。当
好的,我以 root 身份登录到 MySQL 命令行客户端。然后我以 root 身份使用 MySQLdb 模块打开或运行一个 python 应用程序。当我使用 python (IDLE) 检查结果时,
这个问题源于看Rasmus Lerdorf's talk from Drupalcon .顺便说一下,这个问题和他的演讲与 Drupal 没有任何关系……只是在他们的 session 上提出的。我自己
在使用 AJAX 提交创建表单后,我感到非常自豪,我在 IE8 中测试它并得到“消息:‘数量’未定义”。我读到这可能与早期版本的 IE 使用 ActiveX 进行 AJAX 请求有关,但我对 JS 很
对于我的一个项目,我必须对构造函数进行动态调用。但由于这是 Java 7,我使用 java.lang.invoke 而不是“经典”反射 API。 代码: @ParametersAreNonnullBy
基本上,我收到原始时间戳,我需要将它们格式化为 HH:MM:SS 格式。 最佳答案 这是一个提供 UTC 日期灵活格式的函数。它接受类似于 Java 的 SimpleDateFormat 的格式字符串
差不多两周后...我放弃了!!! 我实现了从图库和相机上传图片... startActivityForResult... ok! EXTERNAL_CONTENT_URI... ok! ACTIO
我有六张尺寸为 350x400 (WxH) 的 .jpg 图像。 我想将它们变成质量好(但不太重)且尺寸相同的 avi 文件 我正在做: /var/www/html/folder/ffmpeg/ffm
在我的应用程序中,我正在播放一些电影。我已经检查了电影的信息,它们似乎在 MPMoviePlayerController 应该能够处理的范围内(比特率等) 它们通过 URL 进行流式传输,并且只能在
program UInt32ToVariant; uses Vcl.Dialogs, System.SysUtils, System.Variants; var ui32Val: Cardin
无法弄清楚我的 Electron 项目出了什么问题。以 electron . 开头一个 native 模块(pkcs11js)将失败。见图片: 但运行 npm run dist将构建安装程序就好了,并
我正在使用 boost 文件系统获取当前目录的路径,然后检查该目录是否存在。 is_directory()没问题,但是 exists()在同一条路上失败了,我错过了什么吗? 示例代码(boost 1.
此使用 JFrames 的测试代码按预期工作(将在 10 秒后关闭)。 // **** Imports **** import java.awt.*; import javax.swing.JFram
我有一个相当复杂的 LINQ 查询,它通常很慢,它创建了一个 System.Data.SqlClient.SqlException : "等待操作超时"。 但是,当我记录生成的 SQL(通过将 Tex
这个问题在这里已经有了答案: When does JS interpret {} as an empty block instead of an empty object? (2 个答案) 关闭 5
我正在尝试实现一个哈希表。我收到上述错误,但 AFAIK 我已经正确声明了我所有的结构/typedef。 (我还在学C,sorry)错误发生在我的 while 循环中的 test.c 中,我在其中放置
例如 sprintf(pos,"%f ",cl.snap.ps.origin[0]); //don't start with strcat sprintf(tmp,"%f ",cl.snap.ps.o
好的,这是程序,绝对正确 #include using namespace std; template void Swap(T &a , T &b); int main(){ int i
我是一名优秀的程序员,十分优秀!