在Android中将文件上传到Cloudinary时如何显示进度? [英] How to show progress while upload file to Cloudinary in Android?

查看:47
本文介绍了在Android中将文件上传到Cloudinary时如何显示进度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了将文件上传到Cloudinary的功能。

  Cloudinary cloudinary =新Cloudinary(Constants.CLOUDINARY_URL); 
try {
FileInputStream是= new FileInputStream(new File(filePath));
Uploader uploader = cloudinary.uploader();
Map map = uploader.upload(is,new HashMap());
返回地图;
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}

现在,我想在上传时显示进度条中的上传百分比。
但是我找不到任何回调函数来获取上载百分比。
请帮助我。
谢谢。

解决方案

cprakashagr答案实际上可以创造奇迹。只是为了详细说明他的答案,您需要通过以下操作从github下载最新的cloudinary库:

  git clone https: //github.com/cloudinary/cloudinary_java.git 

然后,您必须根据他的链接:

  https://github.com/cloudinary/cloudinary_java/pull/41 

您不能使用Android Studio进行这些更改,因为这是一个maven项目,因此您将需要使用其他IDE,例如IntelliJ。



这是我所做的更改:


  1. 您将需要添加在该类上:UploadCallback

  2. 您将需要用他的类替换该类Uploader: https://github.com/cprakashagr/cloudinary_java/blob/master/cloudinary-core/src/main/java/com /云inary / Uploader.java

完成此操作后,进入cloudinary_java的主文件夹,然后输入进入终端。转到并输入 mvn install 并按Enter。您将看到mvn将为目录中的不同文件夹构建jar。构建完成后,例如,如果导航到 cloudinary-core 文件夹,您将看到 cloudinary-core jar。



您必须从文件夹中复制 cloudinary-core jar,并将其添加到您的android项目中 libs文件夹放在您的android项目的根目录中。完成操作并显示在其中后,右键单击Android Studios中的jar,然后单击添加为库 ,以便android studios将其添加为依赖项。 / p>

cloudinary-android jar不需要添加为jar,因为您可以从gradle中获取副本。您的最终gradle文件应如下所示:

  compile('com.cloudinary:cloudinary-android:1.2.2') {
排除模块:'cloudinary-core'
}
编译文件('/Users/XXX/Documents/myApp/libs/cloudinary-core-1.4.2-SNAPSHOT.jar')

重建您的android studio项目,您会看到 UploadCallback 现在是可以在android项目中使用的对象。这就是您知道jar构造已成功修改的方式。



现在在服务类中,添加用于cloudinary直接上传的代码。您将需要将代码放入服务类中,因为您无法在UI线程上执行网络操作,并且如果尝试以下操作将会收到错误:

  Map config = new HashMap(); 
config.put( cloud_name, XXX);
Cloudinary mobileCloudinary =新的Cloudinary(config);
Map map = null;
try {
map = mobileCloudinary.uploader()
.uploadLarge(this,
intent.getSerializableExtra(getString(R.string.file)),
ObjectUtils。 asMap( public_id,123),
标签, myphoto,
文件夹, mylibrary,
未签名,true,
upload_preset , XXX),
51200);

} catch(IOException e){
e.printStackTrace();
}
字符串imageUrl = map.get( url)。toString();
Timber.e( imageUrl + imageUrl);

您必须将缓冲区设置为足够大(但不要太大)以确保您的回调是真的叫。当我第一次尝试该代码时,我将缓冲区设置为非常大的值,例如 200000000 ,并且未调用回调,因为上载会一次性完成。如果将其设置为较小的值(例如 2048 ),则会定期调用回调,但是上载会变得很慢。由您决定合适的大小以适合您的应用。对我来说,它是 51200 ,这意味着每上传50kb文件,就会发生回调。



工作完成后,您需要将进度信息从服务类传输回活动类,以便可以在屏幕上显示进度。我使用 messageHandler 来做到这一点。这是我的服务类中的消息方法:

  public void sendMessage(浮动进度){
消息=消息。获得();
message.arg1 = Math.round(progress);
try {
messageHandler.send(message);
} catch(RemoteException e){
e.printStackTrace();
}
}

这是我的 messageHandler 在我的活动类上:

 公共类MessageHandler扩展了处理程序{
@Override
public void handleMessage(final Message message){
Timber.e( transfer: + message.arg1);
}
}


I implemented the uploading function the file to Cloudinary.

Cloudinary cloudinary = new Cloudinary(Constants.CLOUDINARY_URL);
    try {
        FileInputStream is = new FileInputStream(new File(filePath));
        Uploader uploader = cloudinary.uploader();
        Map map = uploader.upload(is, new HashMap());
        return map;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Now I would like to show the uploading percent in progress bar while upload. But I can't find any callback function to get uploading percent. Please help me. Thanks.

解决方案

cprakashagr answer actually works wonders. Just to elaborate on his answer, you will need to download the latest cloudinary library off github by doing this:

git clone https://github.com/cloudinary/cloudinary_java.git

Then you will have to make the changes according to his link:

https://github.com/cloudinary/cloudinary_java/pull/41

You cannot use Android Studio to make these changes as this is a maven project so you will need to use another IDE, like IntelliJ for example.

This is how I made the changes:

  1. You will need to add on the class: UploadCallback
  2. You will need to replace the class Uploader with his class: https://github.com/cprakashagr/cloudinary_java/blob/master/cloudinary-core/src/main/java/com/cloudinary/Uploader.java

Once you have done this, go into the main folder of your cloudinary_java, and then enter into Terminal. Go and type in mvn install and press enter. You will see that mvn will build the jars for the different folders in your directory. After the build is complete, if you navigate into the cloudinary-core folder for example, you will see the cloudinary-core jar.

You must copy the cloudinary-core jar from the folder and add it into your android project "libs" folder in the root of your android project. Once you have done that and it appears there, right click on the jar in Android Studios and click "Add as Library" so that android studios will add it as a dependency.

The cloudinary-android jar does not need to be added as a jar as you can grab a copy off gradle. Your final gradle file should look something like this:

compile('com.cloudinary:cloudinary-android:1.2.2') {
        exclude module: 'cloudinary-core'
    }
    compile files('/Users/XXX/Documents/myApp/libs/cloudinary-core-1.4.2-SNAPSHOT.jar')

Rebuild your android studio project and you will see that the UploadCallback is now an object you can use in your android project. This is the way you know your jar build was successfully modified.

Now inside a service class, add your code for cloudinary direct upload. You will need to put the code inside a service class because you cannot do network operations on a UI thread and you will get an error if you tried:

    Map config = new HashMap();
    config.put("cloud_name", "XXX");
    Cloudinary mobileCloudinary = new Cloudinary(config);
    Map map = null;
    try {
        map = mobileCloudinary.uploader()
                .uploadLarge(this,
                        intent.getSerializableExtra(getString(R.string.file)),
                        ObjectUtils.asMap("public_id", 123),
                                "tags", "myphoto",
                                "folder", "mylibrary",
                                "unsigned", true,
                                "upload_preset", "XXX"),
                        51200);

    } catch (IOException e) {
        e.printStackTrace();
    }
    String imageUrl = map.get("url").toString();
    Timber.e("imageUrl " + imageUrl);

You must set the buffer to be large enough (but not too large) to make sure your callback is actually called. When I first tried this code out, I set the buffer to something really large, like 200000000, and the callback was not called because the upload would have happened all in one go. If you set it to a small value, like 2048, the callback will be called regularly however, the upload will become really slow. It is up to you to determine an adequate size which will work well for your app. For me it was 51200 which means for every 50kb of the file which is uploaded, a callback will occur.

Once it is working, you will need to transmit the progress information from the service class back to your activity class so you can display the progress on screen. I use a messageHandler to do so. This is the message method in my service class:

public void sendMessage(float progress) {
    Message message = Message.obtain();
    message.arg1 = Math.round(progress);
    try {
        messageHandler.send(message);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

This is my messageHandler on my activity class:

public class MessageHandler extends Handler {
    @Override
    public void handleMessage(final Message message) {
        Timber.e("transfer: " + message.arg1);
    }
}

这篇关于在Android中将文件上传到Cloudinary时如何显示进度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆