将图像发送到PHP服务器时,android.content.ContentResolver.openInputStream上的NullPointer异常

时间:2018-06-30 07:39:45

标签: android android-volley android-image

我从教程Android Upload Image to Server using Volley Tutorial修改

在本教程中,当用户选择图像时,脚本直接上传到服务器。因此,我修改了当用户选择一个图像时,该图像首先显示在imageview上,然后,当用户单击“ sendtoserver”按钮时,该图像将与另一个EditText发送到服务器。

首先,我完成了选择图像按钮,并且我选择的图像显示在imageview上。

第二秒,当我单击“ sendtoserver”按钮时,出现关于getContentResolver()的错误,这里是我的MainActivity代码

package com.example.celmira.uploadgambar;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.Settings;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.squareup.picasso.Picasso;

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

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class MainActivity extends AppCompatActivity {

//ImageView to display image selected
ImageView imageView;
//edittext for getting the tags input
EditText editTextTags;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    //initializing views
    imageView = (ImageView) findViewById(R.id.imageView);
    editTextTags = (EditText) findViewById(R.id.editTextTags);

    //mengecek permission, jika blm diijinkan, maka buka setting atau keluar apps
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + getPackageName()));
        finish();
        startActivity(intent);
        return;
    }


    //get image from gallery when clicked
    findViewById(R.id.buttonUploadImage).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //if the tags edittext is empty
            //we will throw input error
            if (editTextTags.getText().toString().trim().isEmpty()) {
                editTextTags.setError("Enter tags first");
                editTextTags.requestFocus();
                return;
            }

            //if everything is ok we will open image chooser
            Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, 100);
        }
    });


    // SENDING IMAGE AND TAG TO SERVER WHEN BUTTON sendtoserver clicked
    findViewById(R.id.sendtoserver).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Uri imageUri = getIntent().getData();

            try {
                //getting bitmap object from uri
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
                Log.d("eek",bitmap.toString());

                //function to send to server
                uploadBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 100 && resultCode == RESULT_OK && data != null) {

        //getting the image Uri
        Uri imageUri = data.getData();
        try {
            //getting bitmap object from uri
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
            //displaying selected image to imageview
            Picasso.get().load(imageUri).into(imageView);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

/*
* The method is taking Bitmap as an argument
* then it will return the byte[] array for the given bitmap
* and we will send this array to the server
* here we are using PNG Compression with 80% quality
* you can give quality between 0 to 100
* 0 means worse quality
* 100 means best quality
* */
public byte[] getFileDataFromDrawable(Bitmap bitmap) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
    return byteArrayOutputStream.toByteArray();
}

private void uploadBitmap(final Bitmap bitmap) {

    //getting the tag from the edittext
    final String tags = editTextTags.getText().toString().trim();

    //our custom volley request
    VolleyMultipartRequest volleyMultipartRequest = new VolleyMultipartRequest(Request.Method.POST, "http://192.168.166.2/MyApi/Api.php?apicall=uploadpic",
            new Response.Listener<NetworkResponse>() {
                @Override
                public void onResponse(NetworkResponse response) {
                    try {
                        JSONObject obj = new JSONObject(new String(response.data));
                        Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }) {

        /*
        * If you want to add more parameters with the image
        * you can do it here
        * here we have only one parameter with the image
        * which is tags
        * */
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("tags", tags);
            return params;
        }

        /*
        * Here we are passing image by renaming it with a unique name
        * */
        @Override
        protected Map<String, DataPart> getByteData() {
            Map<String, DataPart> params = new HashMap<>();
            long imagename = System.currentTimeMillis();
            params.put("pic", new DataPart(imagename + ".png", getFileDataFromDrawable(bitmap)));
            return params;
        }
    };

    //adding the request to volley
    Volley.newRequestQueue(this).add(volleyMultipartRequest);
}
}

,这里是引起错误的脚本

 Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);

这是什么错误消息

FATAL EXCEPTION: main
              java.lang.NullPointerException
                  at android.content.ContentResolver.openInputStream(ContentResolver.java:481)
                  at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:792)
                  at com.example.celmira.uploadgambar.MainActivity$2.onClick(MainActivity.java:94)

如何获取可以传递给MediaStore.Images.Media.getBitmap()的getContentResolver()

1 个答案:

答案 0 :(得分:1)

要在选择iamge后上传:

uploadBitmap(bitmap);移至onActivityResult()

点击按钮后上传:

首先创建全局图片网址

private Uri imageUri;

在代码上方 与您一样在onActivityResult中填写

imageUri = data.getData();

并在通话按钮时发送

findViewById(R.id.sendtoserver).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        //THIS LINE REMOVED
        //Uri imageUri = getIntent().getData();

        try {
            //getting bitmap object from uri
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
            Log.d("eek",bitmap.toString());

            //function to send to server
            uploadBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
});
相关问题