我使用Oracle VirtualBox Debian创建了 OwnCloud 服务器。我希望我的Android应用程序将图像上传到云端。所以我创建了从Gallery获取图像的意图,并使用GitHub的OwnCloud Android库上传它:https://github.com/owncloud/android-library
。我按照示例中的步骤操作,但每次开始上传时,结果都是404
- FILE_NOT_FOUND
以下是我的代码片段:
floatingActionButton.setOnClickListener(v -> {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType(IMAGE_INTENT);
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);
});
}
@Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
profilePicture.setImageBitmap(selectedImage);
File file = new File(getRealPathFromURI(imageUri));
Log.d("Image file", file.getAbsolutePath() + "\t" + file.getName());
handler = new Handler();
Uri serverUri = Uri.parse("http://192.168.1.8/remote.php/webdav/");
client = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true);
client.setCredentials(OwnCloudCredentialsFactory.newBasicCredentials("user", "bitnami"));
client.getParams().setAuthenticationPreemptive(true);
String remotePath = FileUtils.PATH_SEPARATOR + file.getName();
String mimeType = "image/png";
// Get the last modification date of the file from the file system
Long timeStampLong = file.lastModified() / 1000;
String timeStamp = timeStampLong.toString();
UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(file.getAbsolutePath(), remotePath, mimeType, timeStamp);
uploadOperation.addDatatransferProgressListener(this);
uploadOperation.execute(client, this, handler);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) {
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}
@Override
public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String fileAbsoluteName) {
final long percentage = (totalToTransfer > 0 ? totalTransferredSoFar * 100 / totalToTransfer : 0);
handler.post(() -> Log.d("Progress", "progressRate " + percentage));
}
@Override
public void onRemoteOperationFinish(RemoteOperation caller, RemoteOperationResult result) {
Toast.makeText(this, String.valueOf(result.getHttpCode()), Toast.LENGTH_SHORT).show();
}