我正在尝试将图片从我的设备上传到服务器。从图库上传图片工作正常,但从相机上传图片会出现错误
java.io.FileNotFoundException: /storage/emulated/0/1421501712960.jpg: open failed: ENOENT (No such file or directory)
我使用以下代码从相机上传图片。我已经注释掉了我尝试过的其他路径。
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表示您的文件不存在于指定的路径上
检查文件路径
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()
变化很快,因此在您请求相机输入和接收图像之间,值不同,并且找不到文件(这仅当您事先指定文件名时)。
我只能建议阅读和使用文档从相机获取全尺寸图像。唯一的区别是,而不是getExternalStoragePublicDirectory()
,你会使用getExternalFilesDir()
,因为你不希望图像公开。如果您按照指南进行操作,图像路径将存储在String mCurrentPhotoPath;
中,以便您之后轻松上传和删除它。