Android 是否将文件从真实设备上载到服务器?

Android 是否将文件从真实设备上载到服务器?,android,android-asynctask,android-fileprovider,Android,Android Asynctask,Android Fileprovider,我正在开发android应用程序,用户可以从设备中选择文件并上传到服务器。该应用程序在emulator上运行良好,但当我在真实设备上测试它时,并没有得到文件 private void showFileChooser() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); startActivityForResult(intent, PICKFILE_RESULT

我正在开发android应用程序,用户可以从设备中选择文件并上传到服务器。该应用程序在emulator上运行良好,但当我在真实设备上测试它时,并没有得到文件

private void showFileChooser() {


    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    startActivityForResult(intent, PICKFILE_RESULT_CODE);

}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case 1:
            if (resultCode == RESULT_OK) {

                Uri uri = data.getData();
                String uriString = uri.toString();
                File myFile = new File(uriString);
                String path = myFile.getAbsolutePath();
                displayName = null;

                if (uriString.startsWith("content://")) {
                    Cursor cursor = null;
                    try {
                        cursor = this.getContentResolver().query(uri, null, null, null, null);
                        if (cursor != null && cursor.moveToFirst()) {
                            displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                        }
                    } finally {
                        cursor.close();
                    }
                } else if (uriString.startsWith("file://")) {
                    displayName = myFile.getName();

                    Log.i("tagconvertstr", "[" + displayName + "]");
                }
                // txt_file_name_1.setText(displayName);
            }

            txt_file_name_1.setText(displayName);
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}







public class PostDataAsyncTask extends AsyncTask<String, String, String> {


    @Override
    protected String doInBackground(String... strings) {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://pitechnologiesindore.in/pitechno/newfile.php");

            String name = ettname.getText().toString();
            String number = ettnumber.getText().toString();
            String idea = ettidea.getText().toString();
            items = spinner2.getSelectedItem().toString();
            java.util.Date dt = new java.util.Date();
            java.text.SimpleDateFormat sdf =
                    new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            String currentTime = sdf.format(dt);



            File file1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), displayName);
            FileBody fileBody1 = new FileBody(file1);


            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("file1", fileBody1);
            reqEntity.addPart("name", new StringBody(name));
            reqEntity.addPart("number", new StringBody(number));
            reqEntity.addPart("idea", new StringBody(idea));
            reqEntity.addPart("items", new StringBody(items));
            reqEntity.addPart("date", new StringBody(currentTime));
            httpPost.setEntity(reqEntity);

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity resEntity = response.getEntity();

            if (resEntity != null) {
                final String responseStr = EntityUtils.toString(resEntity)
                        .trim();


            }

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

        Log.e("", "RESULT : " + result);

    }
}
private void showFileChooser(){
意向意向=新意向(意向.行动\u获取\u内容);
intent.setType(“*/*”);
startActivityForResult(意图、选择文件、结果、代码);
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
开关(请求代码){
案例1:
if(resultCode==RESULT\u OK){
Uri=data.getData();
字符串uriString=uri.toString();
File myFile=新文件(uriString);
字符串路径=myFile.getAbsolutePath();
displayName=null;
if(uriString.startsWith(“content://”){
游标=空;
试一试{
cursor=this.getContentResolver().query(uri,null,null,null,null);
if(cursor!=null&&cursor.moveToFirst()){
displayName=cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
}最后{
cursor.close();
}
}else if(uriString.startsWith(“文件:/”){
displayName=myFile.getName();
Log.i(“tagconvertstr”,“[“+displayName+”]);
}
//txt_文件_名称_1.setText(显示名称);
}
txt_文件_名称_1.setText(显示名称);
打破
}
super.onActivityResult(请求代码、结果代码、数据);
}
公共类PostDataAsyncTask扩展了AsyncTask{
@凌驾
受保护的字符串背景(字符串…字符串){
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://pitechnologiesindore.in/pitechno/newfile.php");
字符串名称=ettname.getText().toString();
字符串编号=ettnumber.getText().toString();
String idea=ettidea.getText().toString();
items=spinner2.getSelectedItem().toString();
java.util.Date dt=new java.util.Date();
java.text.simpleDataFormat sdf=
新的java.text.simpleDataFormat(“yyyy-MM-dd-HH:MM:ss”);
字符串currentTime=sdf.format(dt);
File file1=新文件(Environment.getExternalStorageDirectory().getAbsolutePath(),displayName);
FileBody fileBody1=新的FileBody(file1);
MultipartEntity reqEntity=新的MultipartEntity(HttpMultipartMode.BROWSER_兼容);
reqEntity.addPart(“文件1”,文件正文1);
reqEntity.addPart(“名称”,新的StringBody(名称));
要求实体添加零件(“编号”,新的StringBody(编号));
REQUENTITY.addPart(“idea”,新StringBody(idea));
REQUENTITY.addPart(“项目”,新的StringBody(项目));
reqEntity.addPart(“日期”,新的StringBody(当前时间));
httpPost.setEntity(reqEntity);
HttpResponse response=httpClient.execute(httpPost);
HttpEntity当前性=response.getEntity();
if(最近性!=null){
最终字符串responsest=EntityUtils.toString(最新)
.trim();
}
}捕获(NullPointerException e){
e、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
Log.e(“,”结果:“+结果);
}
}
我的Php文件

  <?php

  define('HOST','localhost');
  define('USER','****');
  define('PASS','****');
  define('DB','pi1');
  $con = mysqli_connect(HOST,USER,PASS,DB);

$file_path = "resume/";
$file_path = $file_path . basename( $_FILES['file1']['name']);
$fname= $_FILES['file1']['name'];
if(move_uploaded_file($_FILES['file1']['tmp_name'], $file_path)) {

    echo "file saved success";


} else{

   echo "failed to save file";
}



  $name = $_POST['name'];
  $number = $_POST['number'];
  $idea = $_POST['idea'];
  $items = $_POST['items'];
  $date = $_POST['date'];
  $fname= $_FILES['file1']['name'];
$sql = "insert into career (name, age, skill, file_upload, message,date) values ('$name','$number','$items','$fname','$idea','$date')";
  if(mysqli_query($con,$sql)){
    echo 'success';
  }
  else{
    echo 'failure';
  }



?>

您的设备是否具有正常的internet连接?是,我的设备具有internet连接有关displayname的电话。它可以是空的。选择/显示哪一个`文件file1=新文件(环境………..+显示名称)。应该使用file1.exists()查看该文件是否存在。您不知道代码在哪里不起作用。用户可以从设备上的所有位置选择文件,但如果该文件来自新文件(Environment.getExternalStorageDirectory().getAbsolutePath(),displayname)
,则您可以选择。不可能是真的。改用onActivityResult中的内容解析器路径。