在片段中从摄像机捕获图像并使用齐射将其上传到服务器,
当我捕获图像并尝试将其设置为“图像”视图时,会引发以下错误。
11-04 22:44:56.811 3958-3958/? E/libprocessgroup: failed to make
and chown /acct/uid_10058: Read-only file system
11-04 22:45:24.711 3958-3958/com.example.tc E/AndroidRuntime:
FATAL
EXCEPTION: main
Process: com.example.tc, PID: 3958
java.lang.RuntimeException: Failure delivering result
ResultInfo{who=null, request=67424, result=-1, data=Intent {
act=inline-data (has extras) }} to activity
{com.example.tc/com.example.tc.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
......
.......
.......
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
在下面的评论和回答中出现了建议之后,上述错误已修复,但又发生了另一个错误
错误代码低于
2019-11-05 03:28:50.459 1380-2430/com.example.tc E/Volley: [1889] NetworkDispatcher.processRequest: Unhandled exception java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at com.example.tc.ui.slideshow.SlideshowFragment.getStringImage(SlideshowFragment.java:143)
at com.example.tc.ui.slideshow.SlideshowFragment$2$3.getParams(SlideshowFragment.java:102)
at com.android.volley.Request.getBody(Request.java:464)
at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:275)
at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:249)
at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:94)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:123)
at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:131)
at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)
以上错误指示以下几行
bm.compress(Bitmap.CompressFormat.JPEG, 100, ba);
String image = getStringImage(photo);
以下是XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:id="@+id/bClick"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="@id/imgMain"
android:text="Click" />
<Button
android:id="@+id/bUpload"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="@id/bClick"
android:text="Upload" />
<ImageView
android:id="@+id/imgMain"
android:layout_width="300dp"
android:layout_height="300dp"
tools:srcCompat="@tools:sample/avatars[3]" />
以下是更新的Java文件
public class SlideshowFragment extends Fragment {
private SlideshowViewModel slideshowViewModel;
ImageView imgMain;
Button bClick, bUpload;
Bitmap photo;
private static final String URL = "http://192.168.43.215/greetingsImage.php";
private static final int CAMERA_REQUEST = 1888;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_slideshow, container, false);
imgMain = root.findViewById(R.id.imgMain);
final Button bClick = root.findViewById(R.id.bClick);
final Button bUpload = root.findViewById(R.id.bUpload);
bClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
bUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String s = response.trim();
if (!s.equalsIgnoreCase("Loi")) {
Toast.makeText(getActivity(), "Uploaded!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Failed Uploaded!", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
String image = getStringImage(photo);
Map<String, String> params = new HashMap<String, String>();
params.put("IMG", image);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==CAMERA_REQUEST){
Bitmap photo = (Bitmap) data.getExtras().get("data");
if (photo != null) {
Toast.makeText(getActivity(), "Uploaded!", Toast.LENGTH_SHORT).show();
}
imgMain.setImageBitmap(photo);
}
}
public String getStringImage(Bitmap bm){
ByteArrayOutputStream ba = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, ba);
byte[] imagebyte = ba.toByteArray();
String encode = Base64.encodeToString(imagebyte, Base64.DEFAULT);
return encode;
}
}
在上面的代码中,当我注释下一行时,它会上传一个零字节的图像。
bm.compress(Bitmap.CompressFormat.JPEG, 100, ba);
答案 0 :(得分:1)
更改此行 最终的ImageView imgMain = root.findViewById(R.id.imgMain); 与
imgMain = root.findViewById(R.id.imgMain);