我必须将我的图像上传到配置文件片段中的应用程序,所以我使用了两个按钮。一个从图库中选择,一个用于上传。当我执行我的代码时,我可以打开图库来选择图像,但是一旦我点击按钮,我的应用程序就会关闭。
这是我得到的error
:
致命的例外:主要 处理:com.example.hp.truckloaderadmin,PID:32605 java.lang.RuntimeException:传递结果失败 ResultInfo {who = null,request = 1,result = -1,data = Intent { DAT =含量://com.android.providers.media.documents/document/image:5746 flg = 0x1 VirtualScreenParam = Params {mDisplayId = -1,null, mFlags = 0x00000000)}}}到活动 {com.example.hp.truckloaderadmin / com.example.hp.truckloaderadmin.common.common.FragProfile}: java.lang.NullPointerException:尝试调用虚方法'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)'上一个 null对象引用 在android.app.ActivityThread.deliverResults(ActivityThread.java:5008) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:5051) 在android.app.ActivityThread.access $ 1600(ActivityThread.java:229) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1875) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:7410) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 引起:java.lang.NullPointerException:尝试调用虚拟 方法'无效 android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)'上一个 null对象引用 在 com.example.hp.truckloaderadmin.common.common.FragProfile.onActivityResult(FragProfile.java:103) 在android.app.Activity.dispatchActivityResult(Activity.java:7165) 在android.app.ActivityThread.deliverResults(ActivityThread.java:5004) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:5051) 在android.app.ActivityThread.access $ 1600(ActivityThread.java:229) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1875) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:7410) at java.lang.reflect.Method.invoke(Native Method)
这是我的代码:
public class FragProfile extends AppCompatActivity {
Bitmap bitmap;
boolean check = true;
FloatingActionButton imgUp;
FloatingActionButton imgSel;
Button update;
EditText imageName;
ImageView image;
ProgressDialog progressDialog ;
String ImageName = "image_name" ;
String GetImageNameEditText;
String ImagePath = "image_path" ;
String ServerUploadPath ="http://192.168.43.38/internship_project/android_web_services/image.php";
@Nullable
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frag_profile);
imgSel=(FloatingActionButton)findViewById(R.id.fabSelect);
imageName = (EditText)findViewById(R.id.editTextImageName);
imgUp=(FloatingActionButton)findViewById(R.id.fabUpload);
image=(ImageView)findViewById(R.id.image);
imgUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GetImageNameEditText = imageName.getText().toString();
ImageUploadToServerFunction();
}
});
imgSel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image From Gallery"), 1);
}
});
}
@Override
public void onActivityResult(int RC, int RQC, Intent I) {
super.onActivityResult(RC, RQC, I);
if (RC == 1 && RQC == RESULT_OK && I != null && I.getData() != null) {
Uri uri = I.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
image.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void ImageUploadToServerFunction(){
ByteArrayOutputStream byteArrayOutputStreamObject ;
byteArrayOutputStreamObject = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStreamObject);
byte[] byteArrayVar = byteArrayOutputStreamObject.toByteArray();
final String ConvertImage = Base64.encodeToString(byteArrayVar, Base64.DEFAULT);
class AsyncTaskUploadClass extends AsyncTask<Void,Void,String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(FragProfile.this,"Image is Uploading","Please Wait",false,false);
}
@Override
protected void onPostExecute(String string1) {
super.onPostExecute(string1);
// Dismiss the progress dialog after done uploading.
progressDialog.dismiss();
// Printing uploading success message coming from server on android app.
Toast.makeText(FragProfile.this,string1,Toast.LENGTH_LONG).show();
// Setting image as transparent after done uploading.
image.setImageResource(android.R.color.transparent);
}
@Override
protected String doInBackground(Void... params) {
ImageProcessClass imageProcessClass = new ImageProcessClass();
HashMap<String,String> HashMapParams = new HashMap<String,String>();
HashMapParams.put(ImagePath, ConvertImage);
String FinalData = imageProcessClass.ImageHttpRequest(ServerUploadPath, HashMapParams);
return FinalData;
}
}
AsyncTaskUploadClass AsyncTaskUploadClassOBJ = new AsyncTaskUploadClass();
AsyncTaskUploadClassOBJ.execute();
}
public class ImageProcessClass{
public String ImageHttpRequest(String requestURL,HashMap<String, String> PData) {
StringBuilder stringBuilder = new StringBuilder();
try {
URL url;
HttpURLConnection httpURLConnectionObject ;
OutputStream OutPutStream;
BufferedWriter bufferedWriterObject ;
BufferedReader bufferedReaderObject ;
int RC ;
url = new URL(requestURL);
httpURLConnectionObject = (HttpURLConnection) url.openConnection();
httpURLConnectionObject.setReadTimeout(19000);
httpURLConnectionObject.setConnectTimeout(19000);
httpURLConnectionObject.setRequestMethod("POST");
httpURLConnectionObject.setDoInput(true);
httpURLConnectionObject.setDoOutput(true);
OutPutStream = httpURLConnectionObject.getOutputStream();
bufferedWriterObject = new BufferedWriter(
new OutputStreamWriter(OutPutStream, "UTF-8"));
bufferedWriterObject.write(bufferedWriterDataFN(PData));
bufferedWriterObject.flush();
bufferedWriterObject.close();
OutPutStream.close();
RC = httpURLConnectionObject.getResponseCode();
if (RC == HttpsURLConnection.HTTP_OK) {
bufferedReaderObject = new BufferedReader(new InputStreamReader(httpURLConnectionObject.getInputStream()));
stringBuilder = new StringBuilder();
String RC2;
while ((RC2 = bufferedReaderObject.readLine()) != null){
stringBuilder.append(RC2);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
private String bufferedWriterDataFN(HashMap<String, String> HashMapParams) throws UnsupportedEncodingException {
StringBuilder stringBuilderObject;
stringBuilderObject = new StringBuilder();
for (Map.Entry<String, String> KEY : HashMapParams.entrySet()) {
if (check)
check = false;
else
stringBuilderObject.append("&");
stringBuilderObject.append(URLEncoder.encode(KEY.getKey(), "UTF-8"));
stringBuilderObject.append("=");
stringBuilderObject.append(URLEncoder.encode(KEY.getValue(), "UTF-8"));
}
return stringBuilderObject.toString();
}
}
}