Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here it is:</p> <pre><code>public class PictureSaver { private static final String TAG = "PictureSaver"; public static final int MEDIA_TYPE_IMAGE = 1; public static final int MEDIA_TYPE_VIDEO = 2; /** null if unable to save the file */ public static File savePicture(byte[] data, String folder_name) throws SaveFileException { File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE, folder_name); if (pictureFile == null){ Log.d(TAG, "Error creating media file, check storage permissions!"); throw new SaveFileException(TAG, "Error creating media file, check storage permissions!"); } try { FileOutputStream fos = new FileOutputStream(pictureFile); fos.write(data); fos.close(); } catch (FileNotFoundException e) { Log.d(TAG, "File not found: " + e.getMessage()); throw new SaveFileException(TAG, "File not found: " + e.getMessage()); } catch (IOException e) { Log.d(TAG, "Error accessing file: " + e.getMessage()); throw new SaveFileException(TAG, "Error accessing file: " + e.getMessage()); } return pictureFile; } /** Create a File for saving an image or video * null if unable to create the file */ private static File getOutputMediaFile(int type, String folder_name) throws SaveFileException { // To be safe, you should check that the SDCard is mounted // using Environment.getExternalStorageState() before doing this. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), folder_name); // This location works best if you want the created images to be shared // between applications and persist after your app has been uninstalled. // Create the storage directory if it does not exist if (! mediaStorageDir.exists()){ if (! mediaStorageDir.mkdirs()){ Log.d(TAG, "Unable to create directory!"); throw new SaveFileException(TAG, "Unable to create directory!"); } } // Create a media file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); File mediaFile; if (type == MEDIA_TYPE_IMAGE){ mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg"); } else if(type == MEDIA_TYPE_VIDEO) { mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_"+ timeStamp + ".mp4"); } else { throw new SaveFileException(TAG, "Unkknown media type!"); } Log.d(TAG,mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg"); return mediaFile; }} </code></pre> <p>Remember also to do something like:</p> <pre><code>sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); </code></pre> <p>if the picture was correctly saved. This way the next time the user opens the gallery or any other media brwsing application, your newly taken image will be listed correctly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload