Android InputStream Save saveTmpFile(InputStream is)

Here you can find the source of saveTmpFile(InputStream is)

Description

save Tmp File

License

Apache License

Declaration

public static File saveTmpFile(InputStream is) throws Exception 

Method Source Code

//package com.java2s;
/*//from   w w w. j  a  v a 2s  . c o  m
 * Copyright (C) 2012 Kazuya (Kaz) Yokoyama <kazuya.yokoyama@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;
import java.io.OutputStream;

import android.os.Environment;

public class Main {
    public static final String extStorageDirectory = Environment
            .getExternalStorageDirectory().toString() + "/TodoBentoPics/";
    public static final String tmpImageFile = extStorageDirectory
            + "tmp.jpg";

    public static File saveTmpFile(InputStream is) throws Exception {
        File file = getTmpFile();
        OutputStream os = new FileOutputStream(file);

        byte[] buf = new byte[1024];
        int len = 0;

        while ((len = is.read(buf)) > 0) {
            os.write(buf, 0, len);
        }

        os.flush();
        os.close();

        return file;
    }

    public static File getTmpFile() {
        File fileDirectory = new File(extStorageDirectory);
        if (!fileDirectory.exists()) {
            fileDirectory.mkdir();
        }
        return new File(tmpImageFile);
    }
}

Related

  1. saveFile(Context context, String fileName, InputStream inputStream)
  2. writeXmlToTempFile(InputStream xmlStream, String filePath, String closingTag)
  3. writeToString(InputStream stream)
  4. saveInputStreamAsFile(InputStream inputStream, File target)
  5. saveStream(InputStream is, String savePath)
  6. toBase64OutputStream(InputStream is, OutputStream os)
  7. toOutputStream(InputStream is, OutputStream os)
  8. doOutputFile(InputStream is, String filename)
  9. writeFile(File file, InputStream inStream)