我正在尝试将图像发送到我的服务器。我是通过查看LINK
中的教程来做到这一点的然而,我需要发送从相机拍摄的照片,而不是来自画廊,所以我做了一些更改,我得到这样的错误:
致命的例外:主要 处理:com.example.audriusalekna.camerapht,PID:25792 java.lang.RuntimeException:将结果ResultInfo {who = null,request = 1,result = -1,data = Intent {act = inline-data(has extras)}}传递给activity {com.example.audriusalekna.camerapht / com.example.audriusalekna.camerapht.MainActivity}:java.lang.NullPointerException:尝试调用虚方法'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap $ CompressFormat,int,java.io.OutputStream)'on空对象引用 在android.app.ActivityThread.deliverResults(ActivityThread.java:3887) 在android.app.ActivityThread.handleSendResult(ActivityThread.java:3930) 在android.app.ActivityThread.access $ 1300(ActivityThread.java:178) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1561) 在android.os.Handler.dispatchMessage(Handler.java:111) 在android.os.Looper.loop(Looper.java:194) 在android.app.ActivityThread.main(ActivityThread.java:5692) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:959) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap $ CompressFormat,int,java.io.OutputStream)' 在com.example.audriusalekna.camerapht.MainActivity.onActivityResult(MainActivity.java:70) 在android.app.Activity.dispatchActivityResult(Activity.java:6346) 在android.app.ActivityThread.deliverResults(ActivityThread.java:3883) 在android.app.ActivityThread.handleSendResult(ActivityThread.java:3930) 在android.app.ActivityThread.access $ 1300(ActivityThread.java:178) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1561) 在android.os.Handler.dispatchMessage(Handler.java:111) 在android.os.Looper.loop(Looper.java:194) 在android.app.ActivityThread.main(ActivityThread.java:5692) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372)
我在MainActivity的代码在这里:
package com.example.audriusalekna.camerapht;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;
import static android.R.attr.bitmap;
import static android.R.attr.x;
import static android.R.attr.y;
import static android.provider.ContactsContract.CommonDataKinds.Website.URL;
public class MainActivity extends AppCompatActivity {
static final int REQUEST_IMAGE_CAPTURE = 1;
private Uri fileUri;
private ImageView imageView;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button cameraBtn = (Button) findViewById(R.id.push_button);
imageView = (ImageView)findViewById(R.id.imageTest);
cameraBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
final String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
//sending image to server
StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>(){
@Override
public void onResponse(String s) {
if(s.equals("true")){
Toast.makeText(MainActivity.this, "Uploaded Successful", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this, "Some error occurred!", Toast.LENGTH_LONG).show();
}
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(MainActivity.this, "Some error occurred -> "+volleyError, Toast.LENGTH_LONG).show();;
}
}) {
//adding parameters to send
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("image", imageString);
return parameters;
}
};
RequestQueue rQueue = Volley.newRequestQueue(MainActivity.this);
rQueue.add(request);
}
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
//getting image from gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting image to ImageView
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
你的位图为空,为了做你想要的你需要保存文件localy而不是发送它。 在调用相机意图的Onclick上执行以下操作:
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
File file = new File(YOUR_FILE_PATH + "/FILE.jpg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
以后onresult从这样的路径中取出文件:
File f = new File(YOUR_FILE_PATH + "/FILE.jpg");
Bitmap bitmap=null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
try {
bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
希望它有所帮助。
答案 1 :(得分:0)
@greenapps解决方案有效!我刚刚更改了bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos); to photo.compress(Bitmap.CompressFormat.JPEG,100,baos);