本文介绍了使用HTTP Post MultipartEntity从相机上传pics时的java.io.FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将pic从我的设备上传到服务器。从库中上传图片工作正常,但从相机上传图片会出错

I am trying to upload pic from my device to the server.Uploading pic from gallery is working fine but uploading pic from camera gives error

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

我使用以下代码从相机上传图片。我已经注释了我尝试的其他路径。

I am using following code for uploading pic from camera.I have commented out the other paths which i tried.

 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);
            }
        }


推荐答案

Excepstion说你的文件不存在于指定的路径上

Excepstion says that your file donot exists on the specified path

检查文件路径

 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);

这篇关于使用HTTP Post MultipartEntity从相机上传pics时的java.io.FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 09:50