我当前正在使用我的android应用程序中的社区功能,并且正在使用运行良好的Java CloudClient。但是,在将图像上传到Stream.io时遇到了障碍。我正在尝试让用户通过手机将图片上传到他们的画廊,然后将其上传。以下是图库访问代码。 (用Kotlin写)
private fun openGalleryForImage() {
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent, REQUEST_CODE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE){
postImageIV.setImageURI(data?.data) // handle chosen image
image = data?.data //image is the Uri
}
}
获取数据后,我尝试使用以下代码(用Java编写)上传图片
private static URL uploadImage(Uri imageUri) throws StreamException, MalformedURLException {
CloudClient client = CloudClient.builder(apiKey, token, userID).build();
String path = imageUri.getPath();
File imageFile = new File(path).getAbsoluteFile();
URL imageURL = null;
try {
imageURL = client.images().upload(imageFile).join();
}catch(Exception M){
Exception error = M;
System.out.print(error);
}
return imageURL;
}
尽管当我运行本节 client.images()。upload(imageFile).join(); 时,它返回“没有要上传的文件”,我相信这是以下结果的结果CloudClient的上传方法中的代码:
checkArgument(imageFile.exists(), "No file to upload");
我打算以错误的方式将图像上传到Stream.io吗?
答案 0 :(得分:0)
问题是您没有传递有效的文件路径。我创建了一个library,它将为从Uri
返回的MediaStore
返回有效的文件路径。首先,实现该库(“自述文件”中有说明),然后返回此答案。
您将做与现有相同的操作,但需要进行一些细微的更改。在onActivityResult
中,执行以下操作(它是用Java编写的,但是可以轻松更改):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE){
postImageIV.setImageURI(data.getData());
//Pass the Uri to the library
pickiT.getPath(data.getData(), Build.VERSION.SDK_INT);
}
}
然后在PickiTonCompleteListener
中,将路径传递给您所遇到的问题的方法,如下所示:
@Override
public void PickiTonCompleteListener(String path, boolean wasDriveFile, boolean wasUnknownProvider, boolean wasSuccessful, String reason) {
if(wasSuccessful){
//use path to call your uploadImage method
URL yourUrl = uploadImage(path);
}
}
//I changed your method so you can pass the path returned from PickiTonCompleteListener
private static URL uploadImage(String filePath) throws StreamException, MalformedURLException {
CloudClient client = CloudClient.builder(apiKey, token, userID).build();
File imageFile = new File(filePath);
URL imageURL = null;
try {
imageURL = client.images().upload(imageFile).join();
}catch(Exception M){
Exception error = M;
System.out.print(error);
}
return imageURL;
}