光标空对象引用与相机而不是图库

时间:2017-08-16 18:05:12

标签: android android-cursor android-camera-intent

Android上的新功能并遵循本教程:

https://www.simplifiedcoding.net/android-upload-image-using-php-mysql-android-studio/#comment-12178

我能够将从图库和其他一些编辑文本中选择的图片(如姓名,地址等)发送到数据库中。我添加了一些代码,使其也可以使用打开相机的按钮,我可以拍照并保存,但是当我尝试将此信息发送到数据库时,应用程序崩溃了。

我希望有人可以帮助我,我一直在讨价还价。

private Bitmap bitmap;
private Uri filePath;



 private void showFileChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Elige una imagen"), PICK_IMAGE_REQUEST);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == CAM_REQUEST && resultCode == Activity.RESULT_OK ){
        Toast.makeText(TerminosYC.this.getActivity(), "si hay foto", Toast.LENGTH_LONG).show();

 try {
 bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), (filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }

 }else if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK ) {
            filePath = data.getData();

            try {
            bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);

            areaLeyendaImagen.setVisibility(View.VISIBLE);
            areaFotoCargada.setVisibility(View.VISIBLE);
            imagencargada.setVisibility(View.VISIBLE);
            textoimagencargada.setVisibility(View.VISIBLE);
            area_cerrarIagen.setVisibility(View.VISIBLE);
            cerrarImagen.setVisibility(View.VISIBLE);
            buttonChoose.setError(null);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


  public String getPath(Uri uri) {
    Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    String document_id = cursor.getString(0);
    document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
    cursor.close();

    cursor = getActivity().getContentResolver().query(
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
    cursor.moveToFirst();
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
    cursor.close();

    return path;
}

 public class CargarDatos extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... urls) {

        ////////////////-------------
        String correo = campo_correo.getText().toString().trim();
        String nombre = campo_nombre.getText().toString().trim();
        String apellido= campo_apellido.getText().toString().trim();
        String telefono= campo_telefono.getText().toString().trim();
        String categoria= customspinner.getSelectedItem().toString().trim();
        String titulo = campo_titulo.getText().toString().trim();
        String descripcion = campo_descripcion.getText().toString().trim();
        String latitud = Double.toString(latitudeeeee);
        String longitud = Double.toString(longitudeeee);

        String s = (campo_publico.isChecked() ? "1" : "0");
        String r = (campo_terminos.isChecked() ? "1" : "0");

        //getting the actual path of the image
        String path = getPath(filePath);

        //Uploading code
        try {
            String uploadId = UUID.randomUUID().toString();

            //Creating a multi part request
            new MultipartUploadRequest(TerminosYC.this.getActivity(), uploadId, Constants.UPLOAD_URL)
                    .addFileToUpload(path, "image") //Adding file
                    .addParameter("name",correo) //Adding text parameter to the request
                    .addParameter("nombre",nombre)
                    .addParameter("apellido",apellido)
                    .addParameter("telefono", telefono)
                    .addParameter("categoria", categoria)
                    .addParameter("titulo", titulo)
                    .addParameter("descripcion", descripcion)
                    .addParameter("publico", s)
                    .addParameter("terminos", r)
                    .addParameter("latitud",latitud)
                    .addParameter("longitud",longitud)
                    .setNotificationConfig(new UploadNotificationConfig())
                    .setMaxRetries(2)
                    .startUpload(); //Starting the upload

        } catch (Exception exc) {
            Toast.makeText(TerminosYC.this.getActivity(), exc.getMessage(), Toast.LENGTH_SHORT).show();

        }

        // params comes from the execute() call: params[0] is the url.
        try {
            return downloadUrl(urls[0]);
        } catch (IOException e) {
            return "Unable to retrieve web page. URL may be invalid.";
        }
    }


class btnTakePhotoClicker implements  Button.OnClickListener{

    @Override
    public void onClick(View view) {
        //////////////////////////////
        //Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        filePath = Uri.fromFile(getOutputMediaFile());
        intent.putExtra(MediaStore.EXTRA_OUTPUT, filePath);

        startActivityForResult(intent, CAM_REQUEST);
 }
}

我得到以下错误:

08-16 12:58:48.770 7403-7403/com.example.lupitagarcia.yosoyvallarta E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.example.lupitagarcia.yosoyvallarta, PID: 7403
                                                                                  java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference
                                                                                      at com.example.lupitagarcia.yosoyvallarta.TerminosYC.getPath(TerminosYC.java:553)
                                                                                      at com.example.lupitagarcia.yosoyvallarta.TerminosYC$1.onClick(TerminosYC.java:352)
                                                                                      at android.view.View.performClick(View.java:5721)
                                                                                      at android.widget.TextView.performClick(TextView.java:10936)
                                                                                      at android.view.View$PerformClick.run(View.java:22620)
                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:7406)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

更新: 我已经意识到当使用应用程序上的相机拍摄照片时,这不会在画廊中显示,我需要重新启动手机才能看到创建的文件夹上的图片。我不知道这是否可以解决我的问题。

0 个答案:

没有答案