首页 > 解决方案 > 为什么来自相机的图像返回为缩略图?

问题描述

在我的应用程序中,我使用 Koush ION 将图像上传到服务器,现在我的问题是使用相机上传的图像只是缩略图而不是完整的图像 ps。通过图库拍摄的图像是完整图像

在我提供的代码中,我删除了活动中 2 个微调器的所有代码。它们与图像上传端完全分开,因为我使用 Volley 填充它们

所以会发生什么是我用相机拍照并且上传正常问题是上传的图像只是捕获图像的缩略图所以对于我想要的目的来说质量太低了

这是活动的代码

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);

        }


    }
}

标签: javaandroidion-koush

解决方案


推荐阅读