从 Uri 读取 inputStream 并通过 outputStream 发送时出现问题
美好的一天,我正在尝试通过 OutputStream
从 SDCard
发送文件。我打算从我拥有的文件名中获取 URI
,然后使用 InputStream
读取 URI
并转换为其他格式的字节发送。目前,代码卡在 get InputStream
中(如下评论),什么也没有发生。
另外,我不知道我是否正确使用 URI
来获取要发送的文件的实际路径。
请提供任何帮助,我们将不胜感激,因为我已经被困在这里一段时间了。谢谢。
我的代码:
public void sendFile() {
Log.d(TAG, "sending data");
InputStream inputStream = null;
String filePath = Environment.getExternalStorageDirectory()
.toString() + "/" + files.get(0);
Log.d(TAG, "filepath is" + filePath);
Uri uri = Uri.parse(filePath);
Log.d(TAG, "obtained input stream here in Activity");
Log.d(TAG, "Uri here is" + uri); // nothing happens after here, Basically stuck!!
try {
inputStream = getContentResolver().openInputStream(uri);
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
Log.d(TAG, "obtained input stream here in Activity");
int buffersize = 1024;
byte[] buffer = new byte[buffersize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
AppServices.write(byteBuffer.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Good day, I am trying to send a file from an SDCard
through an OutputStream
. I intend to get the URI
from the file name which I have and then use an InputStream
to read the URI
and convert to bytes in other to send. Currently the code is stuck in get InputStream
(As commented below) and nothing happens.
Also, I don't know if I am using the URI
correctly to get the actual path of the file to send.
Please, any help will be greatly appreciated because I have been stuck here for some time now. Thank you.
My code:
public void sendFile() {
Log.d(TAG, "sending data");
InputStream inputStream = null;
String filePath = Environment.getExternalStorageDirectory()
.toString() + "/" + files.get(0);
Log.d(TAG, "filepath is" + filePath);
Uri uri = Uri.parse(filePath);
Log.d(TAG, "obtained input stream here in Activity");
Log.d(TAG, "Uri here is" + uri); // nothing happens after here, Basically stuck!!
try {
inputStream = getContentResolver().openInputStream(uri);
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
Log.d(TAG, "obtained input stream here in Activity");
int buffersize = 1024;
byte[] buffer = new byte[buffersize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
AppServices.write(byteBuffer.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 :
或简单地使用:
Use :
or simply just use:
我尝试使用
InputStream = ctx.getContentResolver().openInputStream(Uri.parse("file://"+fileUri));
但这并不能解决问题。它返回奇怪的字节结果。我尝试以这种方式阅读,但这也不起作用。
为了让它工作,我这样做:
I tried using
InputStream is = ctx.getContentResolver().openInputStream(Uri.parse("file://"+fileUri));
But that won't cut it. It returns wierd byte results. I tried reading this way, but that diden't work either.
To make it work i do like this: