I followed this tutorial to send an image from my phone to the server, however the image never sends. In fact, the app doesn't even attempt to send the image (in logcat). All I see in the logcat is this:
EDIT 2:
I added this piece of code after "requestQueue(request)" and the image successfully sends to the server, but the image_name is not sent to the server for some reason. The file on the server is just ".jpg", no filename.
int x=1;// retry count
request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 48,
x, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}
LOGCAT After EDIT 2:
09-06 20:59:04.530 4188-4194/com.example.android.projectlanguage W/art: Suspending all threads took: 19.394ms
09-06 20:59:10.752 4188-4198/com.example.android.projectlanguage I/art: Background partial concurrent mark sweep GC freed 344902(7MB) AllocSpace objects, 3(39MB) LOS objects, 20% free, 63MB/79MB, paused 1.815ms total 112.441ms
09-06 20:59:16.046 4188-4194/com.example.android.projectlanguage W/art: Suspending all threads took: 7.118ms
09-06 20:59:21.501 4188-9226/com.example.android.projectlanguage D/Volley: [1453] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://128.199.77.211/server/connection.php 0x9c36b58 NORMAL 1> [lifetime=14541], [size=314], [rc=200], [retryCount=0]
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ImageButton ImageButton;
private String encoded_string, image_name;
private Bitmap bitmap;
private File file;
private Uri file_uri;
// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
/**
* Checks if the app has permission to write to device storage
*
* If the app does not has permission then the user will be prompted to grant permissions
*
* @param activity
*/
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton = (ImageButton) findViewById(R.id.camera);
ImageButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
getFileUri();
i.putExtra(MediaStore.EXTRA_OUTPUT,file_uri);
startActivityForResult(i,10);
}
});
}
private void getFileUri() {
image_name = "testing123.jpeg";
file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), image_name);
file_uri = Uri.fromFile(file);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 10 && resultCode == RESULT_OK){
new Encode_image().execute();
}
}
private class Encode_image extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... voids){
bitmap = BitmapFactory.decodeFile(file_uri.getPath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
byte[] array = stream.toByteArray();
encoded_string = Base64.encodeToString(array,0);
return null;
}
@Override
protected void onPostExecute(Void aVoid){
makeRequest();
}
}
private void makeRequest() {
final TextView mTextView = (TextView) findViewById(R.id.text);
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "http://128.199.77.211/server/connection.php";
StringRequest request = new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mTextView.setText(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> map = new HashMap<>();
map.put("encoded_string", encoded_string);
map.put("image_name", image_name);
return map;
}
};
requestQueue.add(request);
}
}
I have made sure to include the volley library in build.gradle as well.
Volley does however set the text to "That didn't work!"
EDIT:
After adding "Log.e("TAG", "err", error);" to onErrorResponse, this is what Logcat produced:
09-05 23:21:44.593 18677-18677/com.example.android.projectlanguage E/TAG: err
com.android.volley.TimeoutError
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:147)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)