Search in sources :

Example 46 with OutputStream

use of java.io.OutputStream in project jpHolo by teusink.

the class ContentFilesystem method copyFileToURL.

@Override
public JSONObject copyFileToURL(LocalFilesystemURL destURL, String newName, Filesystem srcFs, LocalFilesystemURL srcURL, boolean move) throws IOException, InvalidModificationException, JSONException, NoModificationAllowedException, FileExistsException {
    if (LocalFilesystem.class.isInstance(srcFs)) {
        /* Same FS, we can shortcut with CordovaResourceApi operations */
        // Figure out where we should be copying to
        final LocalFilesystemURL destinationURL = makeDestinationURL(newName, srcURL, destURL);
        OutputStream os = resourceApi.openOutputStream(destURL.URL);
        CordovaResourceApi.OpenForReadResult ofrr = resourceApi.openForRead(srcURL.URL);
        if (move && !srcFs.canRemoveFileAtLocalURL(srcURL)) {
            throw new NoModificationAllowedException("Cannot move file at source URL");
        }
        try {
            resourceApi.copyResource(ofrr, os);
        } catch (IOException e) {
            throw new IOException("Cannot read file at source URL");
        }
        if (move) {
            srcFs.removeFileAtLocalURL(srcURL);
        }
        return makeEntryForURL(destinationURL, false, destinationURL.URL.toString());
    } else {
        // Need to copy the hard way
        return super.copyFileToURL(destURL, newName, srcFs, srcURL, move);
    }
}
Also used : OutputStream(java.io.OutputStream) IOException(java.io.IOException) CordovaResourceApi(org.apache.cordova.CordovaResourceApi)

Example 47 with OutputStream

use of java.io.OutputStream in project jpHolo by teusink.

the class CameraLauncher method processResultFromCamera.

/**
     * Applies all needed transformation to the image received from the camera.
     *
     * @param destType          In which form should we return the image
     * @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
     */
private void processResultFromCamera(int destType, Intent intent) throws IOException {
    int rotate = 0;
    // Create an ExifHelper to save the exif data that is lost during compression
    ExifHelper exif = new ExifHelper();
    try {
        if (this.encodingType == JPEG) {
            exif.createInFile(getTempDirectoryPath() + "/.Pic.jpg");
            exif.readExifData();
            rotate = exif.getOrientation();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = null;
    Uri uri = null;
    // If sending base64 image back
    if (destType == DATA_URL) {
        bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));
        if (bitmap == null) {
            // Try to get the bitmap from intent.
            bitmap = (Bitmap) intent.getExtras().get("data");
        }
        // Double-check the bitmap.
        if (bitmap == null) {
            Log.d(LOG_TAG, "I either have a null image path or bitmap");
            this.failPicture("Unable to create bitmap!");
            return;
        }
        if (rotate != 0 && this.correctOrientation) {
            bitmap = getRotatedBitmap(rotate, bitmap, exif);
        }
        this.processPicture(bitmap);
        checkForDuplicateImage(DATA_URL);
    } else // If sending filename back
    if (destType == FILE_URI || destType == NATIVE_URI) {
        if (this.saveToPhotoAlbum) {
            Uri inputUri = getUriFromMediaStore();
            try {
                //Just because we have a media URI doesn't mean we have a real file, we need to make it
                uri = Uri.fromFile(new File(FileHelper.getRealPath(inputUri, this.cordova)));
            } catch (NullPointerException e) {
                uri = null;
            }
        } else {
            uri = Uri.fromFile(new File(getTempDirectoryPath(), System.currentTimeMillis() + ".jpg"));
        }
        if (uri == null) {
            this.failPicture("Error capturing image - no media storage found.");
            return;
        }
        // If all this is true we shouldn't compress the image.
        if (this.targetHeight == -1 && this.targetWidth == -1 && this.mQuality == 100 && !this.correctOrientation) {
            writeUncompressedImage(uri);
            this.callbackContext.success(uri.toString());
        } else {
            bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));
            if (rotate != 0 && this.correctOrientation) {
                bitmap = getRotatedBitmap(rotate, bitmap, exif);
            }
            // Add compressed version of captured image to returned media store Uri
            OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
            bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
            os.close();
            // Restore exif data to file
            if (this.encodingType == JPEG) {
                String exifPath;
                if (this.saveToPhotoAlbum) {
                    exifPath = FileHelper.getRealPath(uri, this.cordova);
                } else {
                    exifPath = uri.getPath();
                }
                exif.createOutFile(exifPath);
                exif.writeExifData();
            }
            if (this.allowEdit) {
                performCrop(uri);
            } else {
                // Send Uri back to JavaScript for viewing image
                this.callbackContext.success(uri.toString());
            }
        }
    } else {
        throw new IllegalStateException();
    }
    this.cleanup(FILE_URI, this.imageUri, uri, bitmap);
    bitmap = null;
}
Also used : Bitmap(android.graphics.Bitmap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Uri(android.net.Uri) File(java.io.File)

Example 48 with OutputStream

use of java.io.OutputStream in project jpHolo by teusink.

the class CameraLauncher method writeUncompressedImage.

/**
     * In the special case where the default width, height and quality are unchanged
     * we just write the file out to disk saving the expensive Bitmap.compress function.
     *
     * @param uri
     * @throws FileNotFoundException
     * @throws IOException
     */
private void writeUncompressedImage(Uri uri) throws FileNotFoundException, IOException {
    FileInputStream fis = new FileInputStream(FileHelper.stripFileProtocol(imageUri.toString()));
    OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
    byte[] buffer = new byte[4096];
    int len;
    while ((len = fis.read(buffer)) != -1) {
        os.write(buffer, 0, len);
    }
    os.flush();
    os.close();
    fis.close();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream)

Example 49 with OutputStream

use of java.io.OutputStream in project jpHolo by teusink.

the class CameraLauncher method ouputModifiedBitmap.

private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
    // Create an ExifHelper to save the exif data that is lost during compression
    String modifiedPath = getTempDirectoryPath() + "/modified.jpg";
    OutputStream os = new FileOutputStream(modifiedPath);
    bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
    os.close();
    // Some content: URIs do not map to file paths (e.g. picasa).
    String realPath = FileHelper.getRealPath(uri, this.cordova);
    ExifHelper exif = new ExifHelper();
    if (realPath != null && this.encodingType == JPEG) {
        try {
            exif.createInFile(realPath);
            exif.readExifData();
            if (this.correctOrientation && this.orientationCorrected) {
                exif.resetOrientation();
            }
            exif.createOutFile(modifiedPath);
            exif.writeExifData();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return modifiedPath;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException)

Example 50 with OutputStream

use of java.io.OutputStream in project jpHolo by teusink.

the class CameraLauncher method onActivityResult.

/**
     * Called when the camera view exits.
     *
     * @param requestCode       The request code originally supplied to startActivityForResult(),
     *                          allowing you to identify who this result came from.
     * @param resultCode        The integer result code returned by the child activity through its setResult().
     * @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
     */
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    // Get src and dest types from request code
    int srcType = (requestCode / 16) - 1;
    int destType = (requestCode % 16) - 1;
    // if camera crop
    if (requestCode == CROP_CAMERA) {
        if (resultCode == Activity.RESULT_OK) {
            // // get the returned data
            Bundle extras = intent.getExtras();
            // get the cropped bitmap
            Bitmap thePic = extras.getParcelable("data");
            if (thePic == null) {
                this.failPicture("Crop returned no data.");
                return;
            }
            // now save the bitmap to a file
            OutputStream fOut = null;
            File temp_file = new File(getTempDirectoryPath(), System.currentTimeMillis() + ".jpg");
            try {
                temp_file.createNewFile();
                fOut = new FileOutputStream(temp_file);
                thePic.compress(Bitmap.CompressFormat.JPEG, this.mQuality, fOut);
                fOut.flush();
                fOut.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // // Send Uri back to JavaScript for viewing image
            this.callbackContext.success(Uri.fromFile(temp_file).toString());
        } else // If cancelled
        if (resultCode == Activity.RESULT_CANCELED) {
            this.failPicture("Camera cancelled.");
        } else // If something else
        {
            this.failPicture("Did not complete!");
        }
    }
    // If CAMERA
    if (srcType == CAMERA) {
        // If image available
        if (resultCode == Activity.RESULT_OK) {
            try {
                this.processResultFromCamera(destType, intent);
            } catch (IOException e) {
                e.printStackTrace();
                this.failPicture("Error capturing image.");
            }
        } else // If cancelled
        if (resultCode == Activity.RESULT_CANCELED) {
            this.failPicture("Camera cancelled.");
        } else // If something else
        {
            this.failPicture("Did not complete!");
        }
    } else // If retrieving photo from library
    if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
        if (resultCode == Activity.RESULT_OK && intent != null) {
            this.processResultFromGallery(destType, intent);
        } else if (resultCode == Activity.RESULT_CANCELED) {
            this.failPicture("Selection cancelled.");
        } else {
            this.failPicture("Selection did not complete!");
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) Bundle(android.os.Bundle) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File)

Aggregations

OutputStream (java.io.OutputStream)4178 IOException (java.io.IOException)1673 FileOutputStream (java.io.FileOutputStream)1331 InputStream (java.io.InputStream)1304 File (java.io.File)925 ByteArrayOutputStream (java.io.ByteArrayOutputStream)830 Test (org.junit.Test)735 BufferedOutputStream (java.io.BufferedOutputStream)477 FileInputStream (java.io.FileInputStream)431 Socket (java.net.Socket)375 ByteArrayInputStream (java.io.ByteArrayInputStream)233 URL (java.net.URL)231 OutputStreamWriter (java.io.OutputStreamWriter)230 HttpURLConnection (java.net.HttpURLConnection)189 BufferedInputStream (java.io.BufferedInputStream)178 InputStreamReader (java.io.InputStreamReader)176 BufferedReader (java.io.BufferedReader)159 FileNotFoundException (java.io.FileNotFoundException)158 GZIPOutputStream (java.util.zip.GZIPOutputStream)157 Path (java.nio.file.Path)155