Ruben Meiring
Ruben Meiring

Reputation: 363

Why does the image from the camera returns as thumbnail?

In my app I use Koush ION to upload images to a server, Now my problem is the images uploaded with the camera are only Thumbnails and not the full Image ps. The Images taken via the gallery are the full image

In the code I provided I removed ALL the code for 2 spinners that are in the activity.They work completely seprate from the image uploading side as I am using Volley to populate them

So What happens is I take the picture with the camera and it uploads fine the problem is the image that is getting uploaded is just a THUMBNAIL of the captured image so It is too low quality for the purpose I want

Heres the code for the activity

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {
    private final int PICK_IMAGE=12345;
    private final int REQUEST_CAMERA=6352;
    private static final int REQUEST_CAMERA_ACCESS_PERMISSION=5674;
    private Bitmap bitmap;
    private ImageView imageView;


    String myURL;
    Spinner spinner;
    Spinner spinner2;
    String URL;
    String URL2;
    ArrayList<String> CategoryName;
    ArrayList<String> ClientName;
    ArrayList<String> Errors;
    String Item;
    String Item2;
    String email;
    String clientId;
    String pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        imageView=findViewById(R.id.imageView);
        Button fromCamera=findViewById(R.id.fromCamera);
        Button fromGallery=findViewById(R.id.fromGallery);
        Button upload=findViewById(R.id.upload);
        CategoryName=new ArrayList<>();
        ClientName=new ArrayList<>();
        Errors=new ArrayList<>();

        spinner=findViewById(R.id.spinner);
        spinner2=findViewById(R.id.spinner2);
        email=getSharedPreferences("MyPrefs", MODE_PRIVATE).getString("name", "");
        clientId=getSharedPreferences("MyPrefs", MODE_PRIVATE).getString("id", "");
        pwd=getSharedPreferences("MyPrefs", MODE_PRIVATE).getString("password", "");


        upload.setOnClickListener(this);
        fromCamera.setOnClickListener(this);
        fromGallery.setOnClickListener(this);


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fromCamera:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                        && ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[]{Manifest.permission.CAMERA},
                            REQUEST_CAMERA_ACCESS_PERMISSION);
                } else {
                    getImageFromCamera();

                }
                break;
            case R.id.fromGallery:
                getImageFromGallery();
                break;
            case R.id.upload:
                if (bitmap != null)
                    uploadImageToServer();

                break;





    private void uploadImageToServer() {

        @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMdd_HH_mm_ss");
        String currentTimeStamp=dateFormat.format(new Date());
        final ProgressDialog pd=new ProgressDialog(SecondActivity.this);
        pd.setMessage("Uploading, Please Wait....");
        pd.show();
        CheckBox chk=findViewById(R.id.chk1);
        if (chk.isChecked()) {
            Uri.Builder builder=new Uri.Builder();
            builder.scheme("https")
                    .authority("www.smartpractice.co.za")
                    .appendPath("files-upload-ruben.asp")
                    .appendQueryParameter("MyForm", "Yes")
                    .appendQueryParameter("ClientID", clientId)
                    .appendQueryParameter("Username", email)
                    .appendQueryParameter("Pwd", pwd)
                    .appendQueryParameter("Category", Item)
                    .appendQueryParameter("ClientName", Item2)
                    .appendQueryParameter("NoEmail", "Yes");
            myURL=builder.build().toString();
        } else {
            Uri.Builder builder4=new Uri.Builder();
            builder4.scheme("https")
                    .authority("www.smartpractice.co.za")
                    .appendPath("files-upload-ruben.asp")
                    .appendQueryParameter("MyForm", "Yes")
                    .appendQueryParameter("ClientID", clientId)
                    .appendQueryParameter("Username", email)
                    .appendQueryParameter("Pwd", pwd)
                    .appendQueryParameter("Category", Item)
                    .appendQueryParameter("ClientName", Item2)
                    .appendQueryParameter("NoEmail", "");

            myURL=builder4.build().toString();
        }


        Toast toast=Toast.makeText(SecondActivity.this, myURL, Toast.LENGTH_LONG);
        toast.show();

        File imageFile=persistImage(bitmap, currentTimeStamp);




        Ion.with(this)
                .load(myURL)
                .uploadProgressDialog(pd)
                .setMultipartFile("SP-LOG", "image/*", imageFile)
                .setMultipartFile("Full","image/*",new File(currentPhotoPath))




                .asString()


                .setCallback(new FutureCallback<String>() {
                    @Override
                    public void onCompleted(Exception e, String result) {
                        pd.cancel();



                    }
                });


    }

    private File persistImage(Bitmap bitmap, String name) {
        File filesDir=getApplicationContext().getFilesDir();
        File imageFile=new File(filesDir, name + ".JPEG");

        OutputStream os;
        try {
            os=new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
            os.flush();
            os.close();
        } catch (Exception e) {
            Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
        }

        return imageFile;
    }


    private void getImageFromCamera() {
        dispatchTakePictureIntent();
        setPic();

        }


    private void getImageFromGallery() {
        Intent intent=new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
        }
    }
    String currentPhotoPath;

    private File createImageFile() throws IOException {

        // Create an image file name
        @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }
    static final int REQUEST_TAKE_PHOTO = 1;

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File

            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.smartpractice.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }

    private void setPic() {
        // Get the dimensions of the View
        int targetW = imageView.getWidth();
        int targetH = imageView.getHeight();

        // Get the dimensions of the bitmap
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;

        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;

        // Determine how much to scale down the image
        int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

        // Decode the image file into a Bitmap sized to fill the View
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;

        bitmap = BitmapFactory.decodeFile(currentPhotoPath, bmOptions);
        imageView.setImageBitmap(bitmap);
    }
    private void galleryAddPic() {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(currentPhotoPath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
    }

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE) {
            if (resultCode == Activity.RESULT_OK) {
                try {
                    InputStream inputStream=getContentResolver().openInputStream(data.getData());
                    bitmap=BitmapFactory.decodeStream(inputStream);
                    imageView.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

            }
        } else if (requestCode == REQUEST_CAMERA) {
            if (resultCode == RESULT_OK) {
                imageView.setImageBitmap(bitmap);
                galleryAddPic();
            }
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CAMERA_ACCESS_PERMISSION) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                getImageFromCamera();
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        }


    }
}

Upvotes: 0

Views: 89

Answers (0)

Related Questions