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