我知道有很多东西要找,但我似乎仍然无法解决我的问题。这是我必须从 imageview 上传图像的代码。imageview 有 mImageUri,这是来自我在这个应用程序中使用的 Aviary 编辑器。
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
@SuppressWarnings("deprecation")
private void doFileUpload(){
String selectedPath = "";
selectedPath = getPath(mImageUri);
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String lineEnd = "rn";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String responseFromServer = "";
String urlString = "http://cliniclowns.boxhost.me/upload.php";
try
{
//------------------ CLIENT REQUEST
FileInputStream fileInputStream = new FileInputStream(new File(selectedPath) );
// open a URL connection to the Servlet
URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name:\"uploadedfile\";filename=\"" + selectedPath + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
Log.e("Debug","File is written");
fileInputStream.close();
dos.flush();
dos.close();
}
catch (MalformedURLException ex)
{
Log.e("Debug", "error: " + ex.getMessage(), ex);
}
catch (IOException ioe)
{
Log.e("Debug", "error: " + ioe.getMessage(), ioe);
}
//------------------ read the SERVER RESPONSE
try {
inStream = new DataInputStream ( conn.getInputStream() );
String str;
while (( str = inStream.readLine()) != null)
{
Log.e("Debug","Server Response "+str);
}
inStream.close();
}
catch (IOException ioex){
Log.e("Debug", "error: " + ioex.getMessage(), ioex);
}
}
有时我得到一个 NullPointerException 有时我的应用程序只是冻结和崩溃。任何有解决方案?
编辑
日志输出:
06-20 13:22:52.724: I/feather-launcher(21073): onCreate
06-20 13:22:52.785: D/dalvikvm(21073): GC_CONCURRENT freed 26K, 46% free 2938K/5379K, external 5132K/5599K, paused 2ms+2ms
06-20 13:22:52.804: D/feather-launcher(21073): Pictures folder: /mnt/sdcard/Pictures
06-20 13:22:52.804: I/feather-launcher(21073): onResume
06-20 13:22:52.934: I/feather-launcher(21073): onCreate
06-20 13:22:52.954: D/feather-launcher(21073): Pictures folder: /mnt/sdcard/Pictures
06-20 13:22:52.954: I/feather-launcher(21073): onResume
06-20 13:22:52.954: D/PhoneWindow(21073): couldn't save which view has focus because the focused view com.android.internal.policy.impl.PhoneWindow$DecorView@2b0018e0 has no id.
06-20 13:22:53.395: I/feather-launcher(21073): onResume
06-20 13:22:56.465: D/feather-launcher(21073): pickRandomImage. total images: 265, position: 60
06-20 13:22:56.465: D/feather-launcher(21073): /mnt/sdcard/DCIM/100ANDRO/DSC_0054.JPG
06-20 13:22:56.465: D/feather-launcher(21073): image uri: /mnt/sdcard/DCIM/100ANDRO/DSC_0054.JPG
06-20 13:22:56.555: D/feather-launcher(21073): width: 400
06-20 13:22:56.669: D/dalvikvm(21073): GC_CONCURRENT freed 147K, 46% free 3087K/5639K, external 5936K/7413K, paused 2ms+2ms
06-20 13:22:56.804: D/feather-launcher(21073): image size: 320x180
06-20 13:22:58.394: D/AndroidRuntime(21073): Shutting down VM
06-20 13:22:58.394: W/dalvikvm(21073): threadid=1: thread exiting with uncaught exception (group=0x2aac8578)
06-20 13:22:58.405: E/AndroidRuntime(21073): FATAL EXCEPTION: main
06-20 13:22:58.405: E/AndroidRuntime(21073): java.lang.NullPointerException
06-20 13:22:58.405: E/AndroidRuntime(21073): at com.project4.LaatsteTabActivity.getPath(LaatsteTabActivity.java:254)
06-20 13:22:58.405: E/AndroidRuntime(21073): at com.project4.LaatsteTabActivity.doFileUpload(LaatsteTabActivity.java:262)
06-20 13:22:58.405: E/AndroidRuntime(21073): at com.project4.LaatsteTabActivity.access$5(LaatsteTabActivity.java:260)
06-20 13:22:58.405: E/AndroidRuntime(21073): at com.project4.LaatsteTabActivity$4.onClick(LaatsteTabActivity.java:128)
06-20 13:22:58.405: E/AndroidRuntime(21073): at android.view.View.performClick(View.java:2552)
06-20 13:22:58.405: E/AndroidRuntime(21073): at android.view.View$PerformClick.run(View.java:9229)
06-20 13:22:58.405: E/AndroidRuntime(21073): at android.os.Handler.handleCallback(Handler.java:587)
06-20 13:22:58.405: E/AndroidRuntime(21073): at android.os.Handler.dispatchMessage(Handler.java:92)
06-20 13:22:58.405: E/AndroidRuntime(21073): at android.os.Looper.loop(Looper.java:130)
06-20 13:22:58.405: E/AndroidRuntime(21073): at android.app.ActivityThread.main(ActivityThread.java:3701)
06-20 13:22:58.405: E/AndroidRuntime(21073): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 13:22:58.405: E/AndroidRuntime(21073): at java.lang.reflect.Method.invoke(Method.java:507)
06-20 13:22:58.405: E/AndroidRuntime(21073): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
06-20 13:22:58.405: E/AndroidRuntime(21073): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
06-20 13:22:58.405: E/AndroidRuntime(21073): at dalvik.system.NativeStart.main(Native Method)