Java tutorial
//package com.java2s; /* * Copyright 2015 The Android Open Source Project * * 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 android.content.Context; import android.net.Uri; import android.util.Log; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; public class Main { private static final String TAG = "TvContractUtils"; private static final boolean DEBUG = true; public static void insertUrl(Context context, Uri contentUri, URL sourceUrl) { if (DEBUG) { Log.d(TAG, "Inserting " + sourceUrl + " to " + contentUri); } InputStream is = null; OutputStream os = null; try { is = sourceUrl.openStream(); os = context.getContentResolver().openOutputStream(contentUri); copy(is, os); } catch (IOException ioe) { Log.e(TAG, "Failed to write " + sourceUrl + " to " + contentUri, ioe); } finally { if (is != null) { try { is.close(); } catch (IOException e) { // Ignore exception. } } if (os != null) { try { os.close(); } catch (IOException e) { // Ignore exception. } } } } public static void copy(InputStream is, OutputStream os) throws IOException { byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1) { os.write(buffer, 0, len); } } }