从 Android 应用空间上传的图像似乎已损坏
在我的 Android 应用程序中,我需要将 Assets/Drawable/raw 文件夹中的图像上传到服务器。 我尝试了以下操作:
InputStream fileInputStream;
if(imageChanged) {
File file = New File("filename");
fileInputStream = new FileInputStream(file);
}else {
fileInputStream = ctx.getAssets().open("default.png");
}
int bytesAvailable;
byte[] buffer = new byte[102400];
while((bytesAvailable = fileInputStream.available()) > 0) {
int bufferSize = Math.min(bytesAvailable, 102400);
if(bufferSize<102400){
buffer = new byte[bufferSize];
}
int bytesRead = fileInputStream.read(buffer, 0,bufferSize);
dos.write(buffer, 0, bytesRead);
}
这执行得很好。我能够读取输入流并将字节写入 DataOutputStream,图像上传到服务器。
无论如何,服务器上的图像似乎已损坏 - 仅适用于默认图像(在“else”块中上传。“if”块图像没有损坏)
我还尝试将default.png放在“raw”文件夹中并尝试了下面
fileInputStream = ctx.getResources().openRawResource(R.drawable.default);
相同的结果 - 服务器上的图像已损坏。
我开始怀疑这是否是因为 default.png 位于应用程序空间中。
我可以获得一些有关在应用程序空间(可绘制/资产/原始)中上传图像的正确方法的帮助吗?
谢谢!
尼米
In my android application, I need to upload a image in my Assets/Drawable/raw folder to the server.
I tried the following:
InputStream fileInputStream;
if(imageChanged) {
File file = New File("filename");
fileInputStream = new FileInputStream(file);
}else {
fileInputStream = ctx.getAssets().open("default.png");
}
int bytesAvailable;
byte[] buffer = new byte[102400];
while((bytesAvailable = fileInputStream.available()) > 0) {
int bufferSize = Math.min(bytesAvailable, 102400);
if(bufferSize<102400){
buffer = new byte[bufferSize];
}
int bytesRead = fileInputStream.read(buffer, 0,bufferSize);
dos.write(buffer, 0, bytesRead);
}
This executes fine. I am able to read the inputstream and write bytes to the DataOutputStream, the image is uploaded to the server.
Anyhow, the image at the server appears to be corrupted - only for the default image (uploaded in the 'else' block. The 'if' block image is not getting corrupted)
I also tried placing default.png in the 'raw' folder and tried the below
fileInputStream = ctx.getResources().openRawResource(R.drawable.default);
Same result here - the image at the server is corrupted.
I am starting to doubt if this is because the default.png is in the application space.
Can I get some help towards the proper way to upload an image in the application space (drawable/asset/raw)?
thanks!
nimi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能与缓冲区大小有关?我尝试了两种不同的方法从资产文件夹中读取/写入 png,并且都生成了工作图像。我使用 FileOutputStream 写入 SD 卡,但这不应该是问题。
It might have to do with the buffer size? I tried two different methods to read/write a png from the assets folder and both produced a working image. I used FileOutputStream to write to the sdcard but that should not be an issue.