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

标签 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()

最佳答案

选择图片后上传:

uploadBitmap(bitmap); 移动到 onActivityResult()

点击按钮后上传:

首先创建全局图片url

私有(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();
        }
    }
});

关于android - 将图像发送到 PHP 服务器时,android.content.ContentResolver.openInputStream 出现 NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51112829/

相关文章:

java - 为什么时间计算不正确?

java - 从未在数据库上明确调用过 Close

java - 将字符串java中的数字相加

java - 从 JSON Volley 请求中获取值的正确方法是什么?

java - 如何使用volley库发送字符串?

java - 如何检查视频播放器是否在appium中启动

java - 如何阅读 POST 截击的响应?

java - 用 Picasso 叠加图像

android - 如何从我的 android 设备而不是 sdcard 获取所有图像和照片?

android - 如何将位图图像转换为 Uri