ms.sujinkim.com.test.api.FoscamSnapshotRunnable.java Source code

Java tutorial

Introduction

Here is the source code for ms.sujinkim.com.test.api.FoscamSnapshotRunnable.java

Source

/**
 * Copyright 2013 Travis Finch
 *
 * 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.
 */

package ms.sujinkim.com.test.api;

import org.apache.http.Header;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import ms.sujinkim.com.test.utils.ToastManager;

public class FoscamSnapshotRunnable implements Runnable {
    private static final String CONTENT_DISPOSITION = "Content-Disposition";

    private FoscamCamera camera;

    public FoscamSnapshotRunnable(FoscamCamera camera) {
        this.camera = camera;
    }

    private void writeSnapshot(String name, InputStream stream) throws IOException {
        Bitmap bitmap = BitmapFactory.decodeStream(stream);
        ByteArrayOutputStream byteStream = null;
        FileOutputStream fileStream = null;
        try {
            byteStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteStream);
            File file = new File(Environment.getExternalStorageDirectory() + File.separator + name);
            file.createNewFile();
            fileStream = new FileOutputStream(file);
            fileStream.write(byteStream.toByteArray());
        } finally {
            if (byteStream != null) {
                byteStream.close();
            }
            if (fileStream != null) {
                fileStream.close();
            }
        }
    }

    public void run() {
        ToastManager manager = ToastManager.getInstance();
        HttpURLConnection conn = null;
        String urlString = FoscamUrlFactory.getSnapshotUrl(camera);
        //DefaultHttpClient client = new DefaultHttpClient();
        try {
            URL url = new URL(urlString);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.connect();
            int status = conn.getResponseCode();
            if (status != 200) {
                manager.makeToast("Unable to take snapshot: " + status);
                conn.disconnect();
                return;
            }

            String name = conn.getHeaderField(CONTENT_DISPOSITION);// ? name?  ?.
            InputStream inputStream = conn.getInputStream();
            if (inputStream != null) {
                writeSnapshot(name, inputStream);
                manager.makeToast("Saved \"" + name + "\"");
            }
        } catch (Exception e) {
            ToastManager.getInstance().makeToast("Unable to take snapshot: " + e.getMessage());
        } finally {
            conn.disconnect();
        }
    }
}