【发布时间】:2022-08-18 17:05:18
【问题描述】:
我正在尝试创建一个应用程序,它将 Byte[] 数组中的图像作为 .png 文件保存到我的 smb2 服务器中,我能够保存一个文件,但它只包含 Array 作为文件名,大小为 0kb。
从相机获取图像
@SuppressLint(\"MissingSuperCall\")
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(requestCode == REQUEST_CODE) {
if (resultCode != RESULT_CANCELED) {
image = (Bitmap) data.getExtras().get(\"data\");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
bytesCapturedLogbook = byteArrayOutputStream.toByteArray();
MyCopy my = new MyCopy();
my.execute(bytesCapturedLogbook);
}
}
}
将文件插入我的服务器的类
private class MyCopy extends AsyncTask<byte[], String, String> {
@Override
protected String doInBackground(byte[]... bytes) {
String z = \"\";
try {
String url = \"smb://000.000.0.000/spm/Image/\" + bytes + \".png\";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
null, \"********\", \"**********\");
SmbFile sfile = new SmbFile(url, auth);
if (!sfile.exists()) {
sfile.createNewFile();
z = \"Created the file for you!!!!\";
} else
z = \"Already exists at the specified location!!!!\";
} catch (Exception ex) {
// TODO: handle exception
z = ex.getMessage().toString();
}
return z;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
@Override
protected void onPostExecute(String r) {
}
}
文件资源管理器中的结果
-
我没有看到您实际尝试将任何字节写入文件的任何地方。请参阅此处了解如何做到这一点:byte[] to file in Java 或参阅this answer using newer NIO features
-
当我尝试了您提供的链接中的所有答案时,没有文件被保存。
-
doInBackground:java.io.FileNotFoundException:004920224616080824:打开失败:EROFS(只读文件系统)004920224616080824是文件名
-
如果您使用保存
doBackground的bytes参数的操作更新问题,将会有所帮助。目前这会创建空文件。顺便说一下,由于...,参数被定义为byte[][]。 -
stackoverflow.com/questions/71394187/…,我像这样更新了我的代码,但我得到了 \'java.io.FileNotFoundException\'
标签: java android-studio jcifs