简体   繁体   English

将图像Uri保存为图像

[英]Save image Uri as an image

So what I am doing is receiving data as an intent from another app. 因此,我正在做的是从另一个应用程序接收数据。 I am getting the image than attempting to save it 我正在获取图像,而不是尝试保存它

  void savefile(Uri sourceuri)
{
    String sourceFilename= sourceuri.getPath();
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "PhotoSaver");


    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;

    try {
        bis = new BufferedInputStream(new FileInputStream(sourceFilename));
        bos = new BufferedOutputStream(new FileOutputStream(mediaStorageDir, false));
        byte[] buf = new byte[1024];
        bis.read(buf);
        do {
            bos.write(buf);
        } while(bis.read(buf) != -1);
    } catch (IOException e) {

    } finally {
        try {
            if (bis != null) bis.close();
            if (bos != null) bos.close();
        } catch (IOException e) {

        }
    }

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "PhotoSaver"))));

}

You should be reading till you reach the end of your input stream and finally flush your output stream. 您应该一直阅读直到到达输入流的末尾,最后刷新输出流。 Something like this: 像这样:

     // the file is read to the end
     while ((value = bis.read()) != -1) {
        bos.write(value);
     }

     // invokes flush to force bytes to be written out to baos
     bos.flush();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM