簡體   English   中英

將圖像Uri保存為圖像

[英]Save image Uri as an image

因此,我正在做的是從另一個應用程序接收數據。 我正在獲取圖像,而不是嘗試保存它

  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"))));

}

您應該一直閱讀直到到達輸入流的末尾,最后刷新輸出流。 像這樣:

     // 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