从 Android 应用空间上传的图像似乎已损坏

发布于 2024-11-03 14:18:29 字数 1112 浏览 1 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大姐,你呐 2024-11-10 14:18:29

这可能与缓冲区大小有关?我尝试了两种不同的方法从资产文件夹中读取/写入 png,并且都生成了工作图像。我使用 FileOutputStream 写入 SD 卡,但这不应该是问题。

InputStream is, is2;
FileOutputStream out = null, out2 = null;
try {
  //method 1: compressing a Bitmap
  is = v.getContext().getAssets().open("yes.png");
  Bitmap bmp = BitmapFactory.decodeStream(is);
  String filename = Environment.getExternalStorageDirectory().toString()+File.separator+"yes.png";
  Log.d("BITMAP", filename);
  out = new FileOutputStream(filename);
  bmp.compress(Bitmap.CompressFormat.PNG, 90, out);

  //method 2: Plain stream IO
  String filename2 = Environment.getExternalStorageDirectory().toString()+File.separator+"yes2.png";
  out2 = new FileOutputStream(filename2);
  Log.d("BITMAP", filename2);
  int r, i=0;
  is2 = v.getContext().getAssets().open("yes.png");
  while ((r = is2.read()) != -1) {
    Log.d ("OUT - byte " + i, "Value: " + r);
    out2.write(r);
    i++;
  }

} catch (IOException e) {
  e.printStackTrace();
} finally {
  try {
    if (out != null)
      out.close();
    if (out2 != null)
      out2.close();
  } catch (IOException e) {
    e.printStackTrace();
  }
}

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.

InputStream is, is2;
FileOutputStream out = null, out2 = null;
try {
  //method 1: compressing a Bitmap
  is = v.getContext().getAssets().open("yes.png");
  Bitmap bmp = BitmapFactory.decodeStream(is);
  String filename = Environment.getExternalStorageDirectory().toString()+File.separator+"yes.png";
  Log.d("BITMAP", filename);
  out = new FileOutputStream(filename);
  bmp.compress(Bitmap.CompressFormat.PNG, 90, out);

  //method 2: Plain stream IO
  String filename2 = Environment.getExternalStorageDirectory().toString()+File.separator+"yes2.png";
  out2 = new FileOutputStream(filename2);
  Log.d("BITMAP", filename2);
  int r, i=0;
  is2 = v.getContext().getAssets().open("yes.png");
  while ((r = is2.read()) != -1) {
    Log.d ("OUT - byte " + i, "Value: " + r);
    out2.write(r);
    i++;
  }

} catch (IOException e) {
  e.printStackTrace();
} finally {
  try {
    if (out != null)
      out.close();
    if (out2 != null)
      out2.close();
  } catch (IOException e) {
    e.printStackTrace();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文