java.io.FileNotFoundException 使用 HTTP 从相机上传照片时 Post MultipartEntity

java.io.FileNotFoundException when Uploading pics from camera using HTTP Post MultipartEntity

我正在尝试将图片从我的设备上传到 server.Uploading 图库中的图片工作正常,但从相机上传图片时出现错误

java.io.FileNotFoundException: /storage/emulated/0/1421501712960.jpg: open failed: ENOENT (No such file or directory)

我正在使用以下代码从 camera.I 上传图片已注释掉我尝试过的其他路径。

 if (requestCode == REQUEST_CAMERA)
                {
                   File f = new File(Environment.getExternalStorageDirectory()
                          .toString());

                    for (File temp : f.listFiles())
                    {
                        if (temp.getName().equals("temp.jpg"))
                        {
                            f = temp;
                            break;
                        }
                    }
                    try {
                        Bitmap bm;
                        BitmapFactory.Options btmapOptions = new BitmapFactory.Options();

                        bm = BitmapFactory.decodeFile(f.getAbsolutePath(),
                                btmapOptions);

                       String path =android.os.Environment
                                .getExternalStorageDirectory().getPath();

                       /*String path = android.os.Environment
                                .getExternalStorageDirectory()
                                + File.separator
                                + "Phoenix" + File.separator + "default";*/



 /*             String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                            Locale.getDefault()).format(new Date());
                    String path = android.os.Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "IMG_" + timeStamp + ".jpg";*/



                       f.delete();

                        File file = new File(path, String.valueOf(System
                                .currentTimeMillis()) + ".jpg");
                        new UpdateProfilePicApi().execute(file.getPath());


                    } catch (Exception e) {
                        e.printStackTrace();
                    }



       public class UpdateProfilePicApi extends AsyncTask<String, Void, String>
        {

            String lOutput="";

            @Override
            public String doInBackground(String... realPath)
            {


                try {
                    File file = new File(realPath[0]);
                    HttpClient mHttpClient = new DefaultHttpClient();
                    HttpPost mHttpPost = new HttpPost(XYZ.URL);
                    mHttpPost.addHeader("X-MZ-Token", mSettingsManager.getAccessToken());

                    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                    entity.addPart("albumId", new StringBody(mHomeActivity.getVideoOption("album_id")));
                    entity.addPart("caption", new StringBody("photo"));
                    entity.addPart("socialType", new StringBody("sharedAlbum"));
                    entity.addPart("pic", new FileBody(file));
                    mHttpPost.setEntity(entity);

                    HttpResponse response = mHttpClient.execute(mHttpPost);

                    lOutput = ""+response.getStatusLine().getStatusCode();

                } catch (Exception e) {

                    lOutput = e.toString();

                }
                return "";
            }

            @Override
            public void onPostExecute(String result)
            {
                mProgressDialog.setVisibility(View.GONE);
                sharedPicsAdapter.notifyDataSetChanged();
                Toast.makeText(getActivity().getApplicationContext(),lOutput,Toast.LENGTH_LONG).show();
                Log.e("loutput",lOutput);

            }

            @Override
            public void onPreExecute()
            {
                mProgressDialog.setVisibility(View.VISIBLE);
            }
        }

异常表明您的文件不存在于指定路径中

检查文件路径

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

        //you can create a new file name "test.jpg" in sdcard folder.
        File f = new File(Environment.getExternalStorageDirectory()
                                + File.separator + "test.jpg");
        try {

            f.createNewFile();
            //write the bytes in file
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
            // remember close de FileOutput
            fo.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (f.exists()) {

            Toast.makeText(this, "Image Found : "+f.getAbsolutePath().toString(), Toast.LENGTH_SHORT).show();
             new UpdateProfilePicApi().execute(f.getAbsolutePath().toString());
        }else{
              Toast.makeText(this, "Image Not Found", Toast.LENGTH_SHORT).show();
        }


    }
}

和你的意图

Intent cameraIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);

问题是 System.currentTimeMillis() 变化很快,所以在您请求相机输入和接收图像之间的值是不同的,您找不到文件(只有当您指定了文件名时事先)。

我只能推荐阅读和使用 documentation for obtaining a full-sized image from camera。唯一的区别是,您将使用 getExternalFilesDir() 而不是 getExternalStoragePublicDirectory(),因为您不想要图像 public。 如果您按照指南操作,图片路径将存储在 String mCurrentPhotoPath; 中,因此您可以轻松上传和删除它。