无法上传相机拍摄的照片

时间:2018-04-13 07:01:33

标签: java android android-studio android-volley multipartform-data

为什么我的活动无法上传相机拍摄的照片,因为它可以轻松上传从画廊中挑选的照片?

通过图库上传的图片可在服务器上获得,但在API命中后,服务器上找不到通过相机拍摄的文件(图片)。

打开服务器上的目录会为通过相机上传的文件显示“找不到文件”错误。

这是我的代码:

import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatCheckBox;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import static com.app.engage.Interfaces.Keys.userDetails.*;

import com.app.engage.Interfaces.ApiResponse;
import com.app.engage.Interfaces.NetworkConnectivityListener;
import com.app.engage.R;
import com.app.engage.Utilities.ConnectionDetector;
import com.app.engage.Utilities.GetServerData;
import com.app.engage.Utilities.ImageOnlyOptionsDialog;
import com.app.engage.Utilities.ProgressDialog;
import com.app.engage.Utilities.RoleSectDialog;
import com.app.engage.Utilities.RoundCorner;
import com.app.engage.Utilities.SharedPreference_Main;
import com.app.engage.Utilities.Utilities;
import com.bumptech.glide.Glide;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import com.app.engage.Interfaces.OnButtonClicked;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class SignupActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener, ApiResponse.Api_hit, View.OnClickListener {
    private TextView toolbarText;
    public static TextView selections;
    private ProgressDialog progressDialog;
    private String selected_role = null;
    private String selectedRole = "", selectedOrgType = "";
    public static String selectedNatPriArea = "";
    private ArrayList<String> role_list, org_type, nat_pri_area;
    private ApiResponse apiResponse;
    private ImageView dp;
    private Uri uriFilePath;
    private int REQUEST_CODE_CAMERA = 1002;
    private int REQUEST_PICK_IMAGE = 1003;
    private AlertDialog.Builder builder;
    private TextView upload_dp;
    private AppCompatCheckBox stayLoggedIn;
    private Spinner roleSpinner, organisationTypeSpinner;
    private Button signUp;
    private SharedPreference_Main sharedPreference_main;
    private String mCurrentPhotoPath;
    //   private Utilities utilities;
    private TextView select_nat_pri_area;
    private ConnectionDetector receiver;
    private EditText fullName, email, orgName, password, reEnterPassword;
    private File file = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        getWindow().setBackgroundDrawableResource(R.drawable.blurred_background);  //setting background here
        android.support.v7.widget.Toolbar toolbar = findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        ActionBar ab = getSupportActionBar();
        role_list = new ArrayList<>();                      //arraylist that will hold the roles coming from the server
        org_type = new ArrayList<>();//arraylist that will hold the roles coming from the server
        nat_pri_area = new ArrayList<>();
        sharedPreference_main = new SharedPreference_Main(this);
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setHomeAsUpIndicator(R.drawable.white_back_icon_for_toolbar);
        toolbarText = findViewById(R.id.toolbarText);
        select_nat_pri_area = findViewById(R.id.nat_pri_areaaa);
        toolbarText.setText("Sign Up");
        apiResponse = new ApiResponse(this);
        roleSpinner = findViewById(R.id.select_role_spinner);
        organisationTypeSpinner = findViewById(R.id.organisation_type_spinner);
        fullName = findViewById(R.id.full_name_editText);
        email = findViewById(R.id.email_editText_signUp);
        password = findViewById(R.id.pass_edit_text);
        reEnterPassword = findViewById(R.id.reEnterPword);
        orgName = findViewById(R.id.org_edit_text);
        selections = findViewById(R.id.selectionss);
        signUp = findViewById(R.id.signUp_Button);
        dp = findViewById(R.id.dp);
        progressDialog = new ProgressDialog(this);
        select_nat_pri_area.setOnClickListener(this);
        upload_dp = findViewById(R.id.upload_dp);
        dp.setOnClickListener(this);
        selections.setOnClickListener(this);
        upload_dp.setOnClickListener(this);
        // utilities = new Utilities(this);


        receiver = new ConnectionDetector(new NetworkConnectivityListener() {
            @Override
            public void onNetworkConnected()                      //if network connection is available
            {

                if (role_list.size() <= 0 || org_type.size() <= 0 || nat_pri_area.size() <= 0) {
                    getData();
                }

            }

            @Override
            public void onNetworkDisconnected() {
                displayAlert();

            }
        }, this);


        // sharedPreference_main = SharedPreference_Main.getInstance(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)    //initialising the AlertDialogue builder
        {
            builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
        } else {
            builder = new AlertDialog.Builder(this);
        }

        roleSpinner.setOnItemSelectedListener(this);
        organisationTypeSpinner.setOnItemSelectedListener(org_type_selection);
        dp.setOnClickListener(this);
        upload_dp.setOnClickListener(this);
//        natPriorityAreaSpinner.setAdapter(adapterSetter(priorityArea));
        signUp.setOnClickListener(this);

    }


    private ArrayAdapter<String> adapterSetter(ArrayList<String> data)   //to temporarily populate the spinners
    {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, R.layout.custom_spinner, data);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        return adapter;
    }


    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(receiver, filter);
        receiver.isNetworkAvailable();
        //checkConnection();
    }

    public void signup() {
        if (!validate()) {     //if the conditions in the validate function are not met return
            onSignupFailed();
            Toast.makeText(this, "Something went wrong sign-up failed", Toast.LENGTH_SHORT).show();
            return;
        } else {
            Toast.makeText(this, "signing up", Toast.LENGTH_SHORT).show();
            //  signUp.setEnabled(false);
            progressDialog.progressDialogStart("Please Wait...");
            try {
                apiResponse.hitVolleyMultipartApi(this, "signup", getparams(), prof_pic, file);
            } catch (Exception e) {
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        }
        // TODO: Implement your own signup logic here.'
        //  save();


    }


    public void onSignupSuccess()    //if signup is successful
    {
        signUp.setEnabled(true);
        setResult(RESULT_OK, null);
        //Toast.makeText(this, "snkflsndkfl", Toast.LENGTH_SHORT).show();
        Intent i = new Intent(this, HomeActivity.class);
        //i.putExtra("Back Functionality", true);
        startActivity(i);
        finish();
    }

    public void onSignupFailed() {
        Toast.makeText(getBaseContext(), "Signup failed", Toast.LENGTH_LONG).show();
        signUp.setEnabled(true);
    }

    public boolean validate()    //to check the entered information
    {
        boolean valid = true;

        String name = fullName.getText().toString();
        String mail = email.getText().toString();
        String pword = password.getText().toString();
        String org_Name = orgName.getText().toString();
        String reEnterPword = reEnterPassword.getText().toString();

        if (name.isEmpty() || name.length() < 3) {
            fullName.setError("at least 3 characters", null);
            valid = false;
        } else {
            fullName.setError(null);
        }
        if (mail.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(mail).matches()) {
            email.setError("enter a valid email address", null);
            valid = false;
        } else {
            email.setError(null);
        }
        if (org_Name.isEmpty()) {
            orgName.setError("Please enter the organisation's name", null);
            valid = false;
        } else {
            orgName.setError(null);
        }


        if (pword.isEmpty() || password.length() < 4 || password.length() > 10) {
            password.setError("between 4 and 10 alphanumeric characters", null);
            valid = false;
        } else {
            password.setError(null);
        }

        if (reEnterPword.isEmpty() || reEnterPassword.length() < 4 || reEnterPassword.length() > 10 || !(reEnterPword.equals(pword))) {
            reEnterPassword.setError("Password Do not match", null);
            valid = false;
        } else {
            reEnterPassword.setError(null);

        }

        return valid;
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        switch (i) {
            case 0:
                selectedRole = "Facilitator";
                break;
            case 1:
                selectedRole = "Provider Administrator";
                break;
            case 2:
                selectedRole = "Provider Executive";
                break;
            case 3:
                selectedRole = "School PLD Administrator";
                break;
            case 4:
                selectedRole = "MOE National Priority";
                break;
            case 5:
                selectedRole = "Senior Leadership";
                break;
            case 6:
                selectedRole = "Teacher";
                break;

        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

    AdapterView.OnItemSelectedListener org_type_selection = new AdapterView.OnItemSelectedListener()    //for organisation type selection
    {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            switch (i) {
                case 0:
                    selectedOrgType = "Education";
                    break;
                case 1:
                    selectedOrgType = "IT";
                    break;
                case 2:
                    selectedOrgType = "Medical";
                    break;
                case 3:
                    selectedOrgType = "";
                    break;
                case 4:
                    selectedOrgType = "";
                    break;
                case 5:
                    selectedOrgType = "";
                    break;
                case 6:
                    selectedOrgType = "";
                    break;

            }
        }


        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    };

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.dp:
            case R.id.upload_dp:
                ImageOnlyOptionsDialog imageOnlyOptionsDialog = new ImageOnlyOptionsDialog();
                imageOnlyOptionsDialog.setonButtonClickListener(new OnButtonClicked() {
                    @Override
                    public void onButtonCLick(int buttonId) {
                        switch (buttonId) {
                            case R.id.btnCamera:
                                startCamera();
                                break;
                            case R.id.btnGallery:
                                pickFromGallery();
                                break;
                            default:
                                break;
                        }
                    }
                });
                imageOnlyOptionsDialog.show(getSupportFragmentManager(), ImageOnlyOptionsDialog.class.getSimpleName());

                break;

            case R.id.signUp_Button:
                signup();
                break;

            case R.id.nat_pri_areaaa:
            case R.id.selectionss:
                Bundle data = new Bundle();
                data.putStringArrayList("pri_areas", nat_pri_area);
                RoleSectDialog roleSectDialog = new RoleSectDialog();
                roleSectDialog.setArguments(data);
                roleSectDialog.show(getSupportFragmentManager(), RoleSectDialog.class.getSimpleName());
                roleSectDialog.setonButtonClickListener(new OnButtonClicked() {
                    @Override
                    public void onButtonCLick(int buttonId) {
                        switch (buttonId) {
                            case R.id.k:
                                break;
                            default:
                                break;
                        }
                    }
                });
                break;
            default:
                break;
        }

    }


    public void startCamera() {
        PackageManager packageManager = this.getPackageManager();
        if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            File mainDirectory = new File(Environment.getExternalStorageDirectory(), "MyFolder/tmp");
            if (!mainDirectory.exists())
                mainDirectory.mkdirs();

            Calendar calendar = Calendar.getInstance();

            uriFilePath = Uri.fromFile(new File(mainDirectory, "IMG_" + calendar.getTimeInMillis() + ".jpeg"));
            Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent1.putExtra(MediaStore.EXTRA_OUTPUT, uriFilePath);
            startActivityForResult(intent1, REQUEST_CODE_CAMERA);
        }
    }

    public void pickFromGallery()            //this is the intent creation for picking image from gallery
    {

        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        startActivityForResult(Intent.createChooser(intent, "Complete action using"), REQUEST_PICK_IMAGE);

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data)            //this receives the intent's result
    {
        if (requestCode == REQUEST_CODE_CAMERA) {
            if (resultCode == RESULT_OK) {
                try {
                    ExifInterface exif = new ExifInterface(uriFilePath.getPath());
                    int orientation = exif.getAttributeInt(
                            ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);
                    Matrix matrix = new Matrix();

                    switch (orientation) {
                        case ExifInterface.ORIENTATION_ROTATE_90:
                            matrix.postRotate(90);
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_180:
                            matrix.postRotate(180);
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_270:
                            matrix.postRotate(270);
                            break;
                    }
                    String filePath = getRealPathFromURI(uriFilePath);
                    file = new File(new URI("file://" + filePath.replace(" ", "%20")));//photo is file type that is global
                    Glide.with(this)
                            .load(file)
                            .placeholder(R.drawable.upload_image)
                            .into(dp);
//                    getImageDetails(photo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else if (requestCode == REQUEST_PICK_IMAGE) {

            if (resultCode == RESULT_OK) {

                Uri selectedImageUri = data.getData();
                String selectedImagePath = getRealPathFromURI(selectedImageUri);
                Toast.makeText(this, selectedImagePath, Toast.LENGTH_SHORT).show();
                try {
                    file = new File(new URI("file://" + selectedImagePath.replace(" ", "%20")));
                    Glide.with(this).load(file).placeholder(R.drawable.upload_image)
                            .into(dp);
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }


            }
        }
    }

    public String getRealPathFromURI(Uri uri) {
        if (uri == null) {
            return null;
        }
        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = this.getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            int column_index = cursor.getColumnIndex(projection[0]);
            return cursor.getString(column_index);
        }
        return uri.getPath();
    }


    public HashMap<String, String> getparams() {                            //parameters passed while hitting the API
        HashMap<String, String> params = new HashMap<>();
        params.put(rulee, "sign_up");
        params.put(emaill, email.getText().toString());
        params.put(word, password.getText().toString());
        params.put(f_name, fullName.getText().toString());
        params.put(n_p_a, selections.getText().toString());
        params.put(or_name, orgName.getText().toString());
        params.put(or_type, selectedOrgType);
        Toast.makeText(this, selectedRole, Toast.LENGTH_SHORT).show();
        params.put(rolId, selectedRole);
        return params;
    }

    @Override
    public void response(String responseObject, String method_call)        //this gets the response when API is hit
    {
        progressDialog.dismissDialog();
        try {
            JSONObject response = new JSONObject(responseObject);
            if (response.getString("flag").equalsIgnoreCase("success")) {
                JSONObject object = response.getJSONObject("response");
                sharedPreference_main.setUserName(object.getString(f_name));
                sharedPreference_main.setUserEmail(object.getString(emaill));
                sharedPreference_main.setUserOrganisation(object.getString(or_name));
                sharedPreference_main.setUserRole(object.getString(rol));
                sharedPreference_main.setOrgType(object.getString(or_type));
                sharedPreference_main.setNPA(object.getString(n_p_a));
                sharedPreference_main.setUserProfilePic(object.getString(prof_pic));
                onSignupSuccess();    //if everything works out call the home activity
            } else if (response.getString("flag").equalsIgnoreCase("0")) {
                String resp = response.getString("response");
                builder.setTitle("Error:");          //building up the dialog
                builder.setMessage(resp);
                displayAlert(resp);
            } else if (response.getString("flag").equalsIgnoreCase("1")) {
                // Toast.makeText(this, response.getString("flag"), Toast.LENGTH_SHORT).show();
                JSONArray data = response.getJSONArray("message");

                if (data.length() > 0) {

                    for (int i = 0; i < data.length(); i++) {
                        JSONObject inData = data.getJSONObject(i);
                        if (inData.has("role_name")) {
                            role_list.add(inData.getString("role_name"));
                        } else if (inData.has("organisation_type")) {
                            org_type.add(inData.getString("organisation_type"));
                        } else if (inData.has("prior_name")) {

                            nat_pri_area.add(inData.getString("prior_name"));
                        }
                        //     N_P_A.add(inData.getString("prior_name"));
                    }
                }
                if (role_list.size() > 0) {
                    // String[] role = role_list.toArray(new String[role_list.size()]);
                    roleSpinner.setAdapter(adapterSetter(role_list));
                }
                if (org_type != null) {
                    //  String organisationType[] =org_type.toArray(new String[org_type.size()]);
                    organisationTypeSpinner.setAdapter(adapterSetter(org_type));
                }

                //   String[] priorityArea = org_type.toArray(new String[org_type.size()]);

                //
            }


            // onSignupSuccess();
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void error(String error) {
        progressDialog.dismissDialog();
        Toast.makeText(this, error, Toast.LENGTH_SHORT).show();

    }

    private void displayAlert(final String code) {
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                fullName.setText("");
                email.setText("");
                password.setText("");
                orgName.setText("");

            }
        }).show();


    }

    private void displayAlert()   //overloaded function
    {
        builder.setTitle("No internet connection");
        builder.setTitle("Please switch on the internet");
        builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //    checkConnection();
                if (receiver.isNetworkAvailable()) {
                    dialogInterface.c**strong text**ancel();
                } else {
                    displayAlert();
                }

            }
        }).setNegativeButton("Exit", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //  checkConnection();
                dialogInterface.cancel();
            }
        }).show();


    }

    public void getData() {
        GetServerData getServerData = new GetServerData(this);
        getServerData.getRoles();
    }
}

注意: 语言版本 - Java 目标SDK -23

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

在您的问题得到解决后使用此功能并进行upvote。

    private File profilePic;
    private Uri photoFileUri;


    /**
     * Method below will work for Camera Intent.
     */
    private void clickPicture() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

            try {
                photoFile = AppUtils.getInstance().createImageFile(); // Create the File where the photo should go
                outputFileUri = FileProvider.getUriForFile(SignUpActivity.this, getApplicationContext().getPackageName() + ".provider", photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                startActivityForResult(takePictureIntent, CAMERA_IMAGE_REQUEST);
            } catch (IOException io) {
                io.printStackTrace(); // Error occurred while creating the File
            }
            //outputFileUri = Uri.fromFile(photoFile);//At this Uri the image captured will get saved.
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {

                case AppConstants.MediaConstants.REQUEST_CAPTURE_IMAGE:
                    profilePic = new            File(AppUtils.getInstance().getRealPathFromURI(photoFileUri, mActivity));
                    break;
            }
        }
    }

直接在您的课程中使用以下方法

/**
     * method to get real path from uri
     *
     * @param contentURI
     * @param context
     * @return
     */
    public String getRealPathFromURI(Uri contentURI, Context context) {
        String result;
        Cursor cursor = context.getContentResolver().query(contentURI, null, null, null, null);
        if (cursor == null) { // Source is Dropbox or other similar local file path
            result = contentURI.getPath();
        } else {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            result = cursor.getString(idx);
            cursor.close();
        }
        return result;
    }

在res目录中的文件夹名“xml”中创建“provide_paths.xml”文件并在其中添加代码

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external_files"
        path="." />

</paths>