Taking a picture via camera and sending it to server in bytearray

后端 未结 2 1977
走了就别回头了
走了就别回头了 2020-12-20 09:32

I am working on an Andorid application in which I would like the user to take a picture and then to save it I am sending it over to the server. Now, I am sending the picture

2条回答
  •  既然无缘
    2020-12-20 10:09

    At first you need to create a file and save image on it and here's a code.

    //method to save image in internal or external storage

    private void storeImage(Bitmap image,String imageName) {
        File pictureFile = getOutputMediaFile(imageName);
        if (pictureFile == null) {
            Log.d(TAG,"Error creating media file, check storage permissions: ");// e.getMessage());
            return;
        } 
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            image.compress(Bitmap.CompressFormat.PNG, 90, fos);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }  
    }
    

    //method to create file to save image on it

    private  File getOutputMediaFile(String imageName){
        //create folder with name FoursquareAPI
        File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
                + "/FoursquareAPI");
    
        // Create the storage directory if it does not exist
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                return null;
            }
        } 
        File mediaFile;
            String mImageName= imageName +".png";
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);  
        return mediaFile;
    }
    

提交回复
热议问题