当从图库中选择照片进行上传时,应用程序崩溃

问题描述:

在我的应用程序中,用户可以将图像上传到服务器,但在某些设备上,如版本为(4.4.4.5.1和5.1.1)的手机,甚至对于从“图库”中选择图像时所有设备的应用程序崩溃。但是当我选择“照片”选项并选择照片并上传时,它可以正常工作,那么如何解决这个问题,并且可以从图库或任何专辑中选择照片而不会崩溃,也可以毫无问题地上传照片?当从图库中选择照片进行上传时,应用程序崩溃

下面给出的是上传图片的全码:

public class send extends AppCompatActivity { 
private Button buttonChoose; 
private Button buttonUpload; 

private ImageView imageView; 

private EditText editTextName; 
private EditText editTextPhone; 
private EditText editTextText; 
private Bitmap bitmap; 

private int PICK_IMAGE_REQUEST = 1; 
private static String host=PathConfig.hostName; 
private String UPLOAD_URL ="http://"+host+"/upload.php"; 

private String KEY_IMAGE = "image"; 
private String KEY_NAME = "name"; 
private String KEY_PHONE = "phone"; 
private String KEY_TEXT = "text"; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_send); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_send); 

    setSupportActionBar(toolbar); 
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.arrowb); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); // setting the back arrow in the toolbar 
    getSupportActionBar().setDisplayShowHomeEnabled(true); 
    setTitle("إرسال شكوى أو إقتراح"); 

    buttonChoose = (Button) findViewById(R.id.chooseButton); 
    buttonUpload = (Button) findViewById(R.id.sendButton); 

    editTextName = (EditText) findViewById(R.id.name); 
    editTextPhone= (EditText) findViewById(R.id.phone); 
    editTextText= (EditText) findViewById(R.id.text); 
    imageView = (ImageView) findViewById(R.id.imageView4); 


    buttonChoose.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      showFileChooser(); 
     } 
    }); 
    buttonUpload.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      uploadImage(); 
     } 
    }); 

} 
public String getStringImage(Bitmap bmp){ 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    if(bmp!=null){ 
     bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    } 
    byte[] imageBytes = baos.toByteArray(); 
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT); 
    return encodedImage; 
} 

private void uploadImage() { 
    final String name = editTextName.getText().toString().trim(); 
    final String phone = editTextPhone.getText().toString().trim(); 
    final String text = editTextText.getText().toString().trim(); 

    if (name.matches("") || phone.matches("") || text.matches("")) { 
     Toast.makeText(this, " عذرا, لا يمكن ترك حقول فارغة", Toast.LENGTH_SHORT).show(); 

    } else { 
     //Showing the progress dialog 
     final ProgressDialog loading = ProgressDialog.show(this, "جاري الإرسال...", "الرجاء الإنتظار...", false, false); 
     StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String s) { 
         //Disimissing the progress dialog 
         loading.dismiss(); 
         //Showing toast message of the response 
         Toast.makeText(send.this, s, Toast.LENGTH_LONG).show(); 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError volleyError) { 
         //Dismissing the progress dialog 
         loading.dismiss(); 

         //Showing toast 
         Toast.makeText(send.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show(); 
        } 
       }) { 
      @Override 
      protected Map<String, String> getParams() throws AuthFailureError { 
       //Converting Bitmap to String 
       String image = getStringImage(bitmap); 

       //Getting Image Name 


       //Creating parameters 
       Map<String, String> params = new HashMap<String, String>(); 

       //Adding parameters 
       params.put(KEY_IMAGE, image); 
       params.put(KEY_NAME, name); 
       params.put(KEY_PHONE, phone); 
       params.put(KEY_TEXT, text); 

       //returning parameters 
       return params; 
      } 
     }; 

     //Creating a Request Queue 
     RequestQueue requestQueue = Volley.newRequestQueue(this); 

     //Adding request to the queue 
     requestQueue.add(stringRequest); 
    } 
} 
private void showFileChooser() { 
    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) { 
     Uri filePath = data.getData(); 
     try { 
      //Getting the Bitmap from Gallery 
      bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath); 
      //Setting the Bitmap to ImageView 
      imageView.setImageBitmap(bitmap); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
@Override 
public boolean onSupportNavigateUp() { 
    onBackPressed(); 
    return true; 

} 

} 
+0

使用logcat的检查与碰撞相关的Java堆栈跟踪:https://*.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare

+0

什么是你的崩溃日志添加问题 –

我觉得你失踪问permission.Can你检查,这是对你有用吗?

profile_img.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (Build.VERSION.SDK_INT >= 23) { 
       // Marshmallow+ 
       if (!checkAccessFineLocationPermission() || !checkAccessCoarseLocationPermission() || !checkWriteExternalStoragePermission()) { 
        requestPermission(); 
       } else { 
        selectImage(); 
       } 
      }else{ 
       selectImage(); 
      } 

     } 
    });