上传照片时出现Android FileNotFoundException
保存后我无法找到照片。 下面是我如何创建从相机拍照并将文件路径保存到相机的意图上传照片时出现Android FileNotFoundException,android,Android,保存后我无法找到照片。 下面是我如何创建从相机拍照并将文件路径保存到相机的意图 private void takePhoto(int position) { Meter meter = adapter.getItem(position); Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File storageDir = Environment.getExternalStora
private void takePhoto(int position)
{
Meter meter = adapter.getItem(position);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File tempFile = createTempFile(meter.id, JPEG_FILE_SUFFIX, storageDir);
if(tempFile != null)
{
lastPhotoPath = tempFile.getAbsolutePath();
Log.d(TAG, "temp picture path=" + lastPhotoPath);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(tempFile));
try
{
startActivityForResult(takePictureIntent, TAKE_PHOTO_REQUEST);
lastPhotoPosition = position;
}
catch(ActivityNotFoundException exc)
{
Log.e(TAG, "activity to take photo not found");
}
}
}
然后稍后我想上传这个图像到服务器。
以下是我是如何做到这一点的
public void compose(OutputStream out) throws DataStorageException
{
Log.d("MainMenuActivity", "file not found in path " + path);
InputStream in = null;
try
{
in = new BufferedInputStream(new FileInputStream(path));
// TODO: there is a better way
byte[] buf = new byte[8192];
while(true)
{
int length = in.read(buf);
if(length < 0)
break;
out.write(buf, 0, length);
}
}
catch(FileNotFoundException exc)
{
throw new DataStorageInternalErrorException("FileNotFoundException, FileRequestComposer compose");
}
catch(IOException exc)
{
// TODO: probably network error
throw new DataStorageInternalErrorException("IOException, FileRequestComposer compose");
}
finally
{
if(in != null)
{
try
{
in.close();
}
catch(IOException exc)
{
// FIXME
}
}
}
}
问题可能与创建文件的方式有关。不要尝试常规构造函数:
File tempFile = new File(storageDir, meter.id + JPEG_FILE_SUFFIX);
通常,您还需要确保您的
storageDir
文件夹存在,并在需要时使用创建它。添加此路径以在SD卡上存储图像
final String uploadFilePath = "/mnt/sdcard/";
你能在这里分享你的清单文件吗?试着添加
,同时给我几分钟时间来测试代码,改为使用这行:file tempFile=new file(storageDir,meter.id+JPEG\u file\u后缀)代码>并查看拍摄后是否存储了照片。@kiruwka。是的,现在我可以在我的图片文件夹中看到这个文件了。@kiruwka,等等。钢铁测试,我不知道发生了什么。但他只保存了我的照片一次。下一次我试着使用它时,我什么也没得到。@Anatol听起来很奇怪。尝试此操作:手动从/mnt/sdcard/pictures/
中删除以前创建的图片,用上面的代码重新安装应用程序,然后重试。
final String uploadFilePath = "/mnt/sdcard/";