私はimgur.comの例と、画像をimgur.comにアップロードする方法についてstackoverflow.comで読んでいます。
画像をアップロードするには、次のコードを使用します。
Bitmap bitmap = image;
// Creates Byte Array from picture
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); // Not sure whether this should be jpeg or png, try both and see which works best
URL url = new URL("http://api.imgur.com/2/upload");
//encodes picture with Base64 and inserts api key
String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(Base64.encode(baos.toByteArray(), Base64.DEFAULT).toString(), "UTF-8");
data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(API_KEY, "UTF-8");
// opens connection and sends data
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
このコードではエラーは発生しません。しかし、画像へのURLを取得することになっているこの次のコードを追加するとき。nullpointer例外が発生します。次のコードはwr.flush()の下にあります。
BufferedReader in = new BufferedReader(
new InputStreamReader(
conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
なぜ私がこのNPEを手に入れるのか誰かが知っていますか?またはこれを行うためのより良い方法?
08-22 04:58:52.810: E/AndroidRuntime(26104): FATAL EXCEPTION: main
08-22 04:58:52.810: E/AndroidRuntime(26104): java.lang.NullPointerException
08-22 04:58:52.810: E/AndroidRuntime(26104): at se.xflash.myapp.ViewContact.uploadImgur(ViewContact.java:540)
08-22 04:58:52.810: E/AndroidRuntime(26104): at se.xflash.myapp.ViewContact.onOptionsItemSelected(ViewContact.java:340)
08-22 04:58:52.810: E/AndroidRuntime(26104): at android.app.Activity.onMenuItemSelected(Activity.java:2507)
08-22 04:58:52.810: E/AndroidRuntime(26104): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:979)
08-22 04:58:52.810: E/AndroidRuntime(26104): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
08-22 04:58:52.810: E/AndroidRuntime(26104): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
08-22 04:58:52.810: E/AndroidRuntime(26104): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
08-22 04:58:52.810: E/AndroidRuntime(26104): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:468)
08-22 04:58:52.810: E/AndroidRuntime(26104): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:126)
08-22 04:58:52.810: E/AndroidRuntime(26104): at android.view.View$PerformClick.run(View.java:14329)
08-22 04:58:52.810: E/AndroidRuntime(26104): at android.os.Handler.handleCallback(Handler.java:605)
08-22 04:58:52.810: E/AndroidRuntime(26104): at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 04:58:52.810: E/AndroidRuntime(26104): at android.os.Looper.loop(Looper.java:137)
08-22 04:58:52.810: E/AndroidRuntime(26104): at android.app.ActivityThread.main(ActivityThread.java:4511)
08-22 04:58:52.810: E/AndroidRuntime(26104): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 04:58:52.810: E/AndroidRuntime(26104): at java.lang.reflect.Method.invoke(Method.java:511)
08-22 04:58:52.810: E/AndroidRuntime(26104): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
08-22 04:58:52.810: E/AndroidRuntime(26104): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
08-22 04:58:52.810: E/AndroidRuntime(26104): at dalvik.system.NativeStart.main(Native Method)
これは完全なコードです:
public void uploadImgur() {
String API_KEY = "***********************";
String uploadpath = ((MyVariables) getApplication()).getUploadPath();
Bitmap bitmap = BitmapFactory.decodeFile(uploadpath);
// Creates Byte Array from picture
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
try {
URL url = new URL("http://api.imgur.com/2/upload");
String data = URLEncoder.encode("image", "UTF-8")
+ "="
+ URLEncoder.encode(
Base64.encode(baos.toByteArray(), Base64.DEFAULT)
.toString(), "UTF-8");
data += "&" + URLEncoder.encode("key", "UTF-8") + "="
+ URLEncoder.encode(API_KEY, "UTF-8");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
Log.e("wr.write", "Writing data: " + data);
// Get the response
Log.e("GET THE RESPONSE", "Debug 1");
BufferedReader in = new BufferedReader(
new InputStreamReader(
conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
Log.e("Uploadpath is: ", inputLine);
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("get response", "CRASH");
}
}