【问题标题】:Android cloudinary async uploader errorAndroid cloudinary 异步上传器错误
【发布时间】:2014-11-06 23:49:26
【问题描述】:

我正在使用 cloudinary 上传图片。

当我尝试在onCreate 中执行此操作时,它给出了NetworkOnMainThread 的明显错误。所以现在我尝试使用AsyncTask,但现在结果是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)

查看的代码如下:

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;
}

}

关注的行是:

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

【问题讨论】:

  • 找出什么是空的(可能是cloudinary)并确保它不是。

标签: android android-asynctask cloudinary


【解决方案1】:

您已经定义了两次cloudinary——一次在课堂上,一次在onCreate()。当您为 onCreate() 中的值分配一个值时,该类中的 cloudinary 成员仍然为空,因此您的应用程序崩溃。最好将实例传入AsyncTask,如:

onCreate()

Cloudinary cloudinary = new Cloudinary(config);

cloudinary = new Cloudinary(config);

Upload 更改为:

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

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

doInBackground() 中,更改:

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

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

最后,将您的onClick() 更改为

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

}

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

}

【讨论】:

    猜你喜欢
    • 2015-08-20
    • 2020-01-14
    • 2017-04-09
    • 2017-05-05
    • 1970-01-01
    • 2019-01-05
    • 2015-10-02
    • 2015-08-26
    • 1970-01-01
    相关资源
    最近更新 更多