如何将 activity 帖子和个人资料图片的文件上传到 Stream.io
How to upload file to Stream.io for activity posts and profile pictures
我目前正在我的 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(); 它返回 "No file to upload" 我认为这是 CloudClient 上传方法中以下代码的结果:
checkArgument(imageFile.exists(), "No file to upload");
我上传图片到 Stream.io 的方式不对吗?
问题是您没有传递有效的文件路径。我创建了一个 library,它将 return 一个 Uri
的有效文件路径,return 从 MediaStore
编辑。首先,实现库("ReadMe"中有说明)然后return到这个答案。
你会做同样的事情,做一些小的改变。在 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;
}
我目前正在我的 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(); 它返回 "No file to upload" 我认为这是 CloudClient 上传方法中以下代码的结果:
checkArgument(imageFile.exists(), "No file to upload");
我上传图片到 Stream.io 的方式不对吗?
问题是您没有传递有效的文件路径。我创建了一个 library,它将 return 一个 Uri
的有效文件路径,return 从 MediaStore
编辑。首先,实现库("ReadMe"中有说明)然后return到这个答案。
你会做同样的事情,做一些小的改变。在 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;
}