从 Uri 读取 inputStream 并通过 outputStream 发送时出现问题

发布于 2024-12-25 15:04:33 字数 1542 浏览 2 评论 0原文

美好的一天,我正在尝试通过 OutputStreamSDCard 发送文件。我打算从我拥有的文件名中获取 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 技术交流群。

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

发布评论

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

评论(2

合约呢 2025-01-01 15:04:33

使用 :

Uri uri = Uri.fromFile(new File(filePath));

或简单地使用:

inputStream  = new FileInputStream(filePath);

Use :

Uri uri = Uri.fromFile(new File(filePath));

or simply just use:

inputStream  = new FileInputStream(filePath);
暮光沉寂 2025-01-01 15:04:33

我尝试使用
InputStream = ctx.getContentResolver().openInputStream(Uri.parse("file://"+fileUri));
但这并不能解决问题。它返回奇怪的字节结果。我尝试以这种方式阅读,但这也不起作用。

while (is.available() > 0) {
            outputStream.write(is.read(b));
}

为了让它工作,我这样做:

fileUri 参数是一个字符串,例如:“/mnt/sdcard/DCIM/Camera/2013-05-07 15.53.47.jpg”

public static byte[] getImageAsBytes(String fileUri) throws IOException {
    if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        return null;
    }
    //Get file stream
    FileInputStream is = new FileInputStream(fileUri);
    //create output stream
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    //create buffer
    byte[] b = new byte[1024];
    //Read the input to the bytestream
    int len = 0;
    while((len = is.read(b)) != -1){
        outputStream.write(b, 0, len);
    }
    is.close();
    //convert to bytearray
    final byte[] byteArray = outputStream.toByteArray();
    outputStream.close();
    return byteArray;
}

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.

while (is.available() > 0) {
            outputStream.write(is.read(b));
}

To make it work i do like this:

The fileUri param is a string like: "/mnt/sdcard/DCIM/Camera/2013-05-07 15.53.47.jpg"

public static byte[] getImageAsBytes(String fileUri) throws IOException {
    if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        return null;
    }
    //Get file stream
    FileInputStream is = new FileInputStream(fileUri);
    //create output stream
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    //create buffer
    byte[] b = new byte[1024];
    //Read the input to the bytestream
    int len = 0;
    while((len = is.read(b)) != -1){
        outputStream.write(b, 0, len);
    }
    is.close();
    //convert to bytearray
    final byte[] byteArray = outputStream.toByteArray();
    outputStream.close();
    return byteArray;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文