Android的cloudinary异步上传错误 [英] Android cloudinary async uploader error

查看:273
本文介绍了Android的cloudinary异步上传错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用cloudinary上传图像。

当我试图做到这一点在的onCreate 这给了 NetworkOnMainThread 的错误明显的错误。所以,现在我试图使用的AsyncTask ,但现在它导致 NPE

  09-12 23:36:18.443:E / AndroidRuntime(24715):显示java.lang.NullPointerException:产生的原因
09-12 23:36:18.443:E / AndroidRuntime(24715):在com.example.dbms.UploadActivity $ Upload.doInBackground(UploadActivity.java:51)

和考虑到code是如下:

 公共类UploadActivity延伸活动{私人ProgressDialog对话框= NULL;
Cloudinary cloudinary;
结果的JSONObject;
串FILE_PATH;
档案文件;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_upload);    意向I = getIntent();
    FILE_PATH = i.getStringExtra(文件);
    HashMap的配置=新的HashMap();
    config.put(cloud_name,DBMS);
    config.put(API_KEY,键); //我已经改变了密钥和密码
    config.put(api_secret,秘密);
    Cloudinary cloudinary =新Cloudinary(配置);
    TextView中的TextView =(的TextView)findViewById(R.id.text5);
    textView.setText(FILE_PATH);
    文件=新的文件(FILE_PATH);
}私有类上传扩展的AsyncTask<弦乐,太虚,字符串> {
    @覆盖
    保护字符串doInBackground(字符串的URL ...){
        串响应=;        尝试{
            结果= cloudinary.uploader()。上传(文件,
                    Cloudinary.emptyMap());
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }
        返回响应;
    }    @覆盖
    保护无效onPostExecute(字符串结果){
        TextView中的TextView =(的TextView)findViewById(R.id.text5);
        textView.setText(文件上传);
    }
}公共无效的onClick(查看视图){
    上传任务=新上传();
    task.execute(新的String [] {http://www.vogella.com});}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.upload,菜单);
    返回true;
}}

在关注该生产线是:

 结果= cloudinary.uploader()。上传(文件,
                    Cloudinary.emptyMap());


解决方案

您已经定义 cloudinary 两次 - 一次在课堂上,和曾经在的onCreate()。当您将值分配给一个在的onCreate() cloudinary 成员在类依然为空,那么您的应用程序崩溃。这将是最好的实例传递到AsyncTask的,如:

的onCreate()更改

  Cloudinary cloudinary =新Cloudinary(配置);

  cloudinary =新Cloudinary(配置);

修改上传来:

 私有类上传扩展的AsyncTask<弦乐,太虚,字符串> {
    私人Cloudinary mCloudinary;    公共上传(Cloudinary cloudinary){
        超();
        mCloudinary = cloudinary;
    }

doInBackground(),更改:

 结果= cloudinary.uploader()。上传(文件,
                    Cloudinary.emptyMap());

 结果= mCloudinary.uploader()。上传(文件,
                    Cloudinary.emptyMap());

最后,修改你的的onClick()

 公共无效的onClick(查看视图){
    上传任务=新上传();
    task.execute(新的String [] {http://www.vogella.com});}

 公共无效的onClick(查看视图){
    上传任务=新上传(cloudinary);
    task.execute(新的String [] {http://www.vogella.com});}

I am using cloudinary for uploading images.

When I tried to do it in onCreate it gave error obvious error of NetworkOnMainThread. So now I tried using AsyncTask, But now it results in NPE.

09-12 23:36:18.443: E/AndroidRuntime(24715): Caused by: java.lang.NullPointerException
09-12 23:36:18.443: E/AndroidRuntime(24715):    at com.example.dbms.UploadActivity$Upload.doInBackground(UploadActivity.java:51)

And the code in view is as below :

public class UploadActivity extends Activity {

private ProgressDialog dialog = null;
Cloudinary cloudinary;
JSONObject Result;
String file_path;
File file;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_upload);

    Intent i = getIntent();
    file_path = i.getStringExtra("file");
    HashMap config = new HashMap();
    config.put("cloud_name", "dbms");
    config.put("api_key", "key");//I have changed the key and secret
    config.put("api_secret", "secret");
    Cloudinary cloudinary = new Cloudinary(config);
    TextView textView = (TextView) findViewById(R.id.text5);
    textView.setText(file_path);
    file = new File(file_path);
}

private class Upload extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        String response = "";

        try {
            Result = cloudinary.uploader().upload(file,
                    Cloudinary.emptyMap());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        TextView textView = (TextView) findViewById(R.id.text5);
        textView.setText("file uploaded");
    }
}

public void onClick(View view) {
    Upload task = new Upload();
    task.execute(new String[] { "http://www.vogella.com" });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.upload, menu);
    return true;
}

}

The line in concern is :

Result = cloudinary.uploader().upload(file,
                    Cloudinary.emptyMap());

解决方案

You've defined cloudinary twice -- once in the class, and once in onCreate(). When you assign a value to the one in onCreate() the cloudinary member in the class remains null, so your app crashes. It would be best to pass the instance into the AsyncTask, such as:

In onCreate() change

Cloudinary cloudinary = new Cloudinary(config);

to

cloudinary = new Cloudinary(config);

Change Upload to:

private class Upload extends AsyncTask<String, Void, String> {
    private Cloudinary mCloudinary;

    public Upload( Cloudinary cloudinary ) {
        super();
        mCloudinary = cloudinary;
    }

and in doInBackground(), change:

Result = cloudinary.uploader().upload(file,
                    Cloudinary.emptyMap());

to

Result = mCloudinary.uploader().upload(file,
                    Cloudinary.emptyMap());

Finally, change your onClick() from

public void onClick(View view) {
    Upload task = new Upload();
    task.execute(new String[] { "http://www.vogella.com" });

}

to

public void onClick(View view) {
    Upload task = new Upload( cloudinary );
    task.execute(new String[] { "http://www.vogella.com" });

}

这篇关于Android的cloudinary异步上传错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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