我正在使用cloudinary上傳圖片。Android 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'),並確保它不是」噸。 – 323go 2014-09-12 20:20:01