[英]android - java.lang.NullPointerException
我是android編程的新手。 我正在一個項目中,我必須將圖像從android發送到php服務器。 直到今天,當我再次運行該項目時,代碼都運行良好,我收到了這些錯誤。 我不知道我的代碼出了什么問題。 請幫我
這些是錯誤
02-22 23:22:44.117: I/System.out(12070): fileName is null
02-22 23:22:44.117: D/AndroidRuntime(12070): Shutting down VM
02-22 23:22:44.117: W/dalvikvm(12070): threadid=1: thread exiting with uncaught exception (group=0x40018578)
02-22 23:22:44.117: E/AndroidRuntime(12070): FATAL EXCEPTION: main
02-22 23:22:44.117: E/AndroidRuntime(12070): java.lang.NullPointerException
02-22 23:22:44.117: E/AndroidRuntime(12070): at me.company.backup.MainActivity.upload(MainActivity.java:94)
02-22 23:22:44.117: E/AndroidRuntime(12070): at me.company.backup.MainActivity$1.onClick(MainActivity.java:58)
02-22 23:22:44.117: E/AndroidRuntime(12070): at android.view.View.performClick(View.java:2485)
02-22 23:22:44.117: E/AndroidRuntime(12070): at android.view.View$PerformClick.run(View.java:9080)
02-22 23:22:44.117: E/AndroidRuntime(12070): at android.os.Handler.handleCallback(Handler.java:587)
02-22 23:22:44.117: E/AndroidRuntime(12070): at android.os.Handler.dispatchMessage(Handler.java:92)
02-22 23:22:44.117: E/AndroidRuntime(12070): at android.os.Looper.loop(Looper.java:130)
02-22 23:22:44.117: E/AndroidRuntime(12070): at android.app.ActivityThread.main(ActivityThread.java:3687)
02-22 23:22:44.117: E/AndroidRuntime(12070): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 23:22:44.117: E/AndroidRuntime(12070): at java.lang.reflect.Method.invoke(Method.java:507)
02-22 23:22:44.117: E/AndroidRuntime(12070): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-22 23:22:44.117: E/AndroidRuntime(12070): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
02-22 23:22:44.117: E/AndroidRuntime(12070): at dalvik.system.NativeStart.main(Native Method)
這是我的代碼
public class MainActivity extends Activity{
Uri currImageURI;
ProgressDialog dialog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//upload button
Button upload_btn = (Button) this.findViewById(R.id.uploadButton);
upload_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
upload();
}});
}
public void upload(){
ArrayList<Uri> fileName = getFileList();
for ( int i = 0 ; i < fileName.size() ; i++ )
{
HttpUploader uploader = new HttpUploader();
try {
uploader.execute(getRealPathFromURI(fileName.get(i))).get();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
public String getRealPathFromURI(Uri contentUri) {
String [] proj={MediaStore.Images.Media.DATA};
android.database.Cursor cursor = managedQuery( contentUri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
private ArrayList<Uri> getFileList()
{
ArrayList<Uri> fileList = new ArrayList<Uri>();
try
{
String[] proj = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
Cursor actualimagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
null, null, MediaStore.Images.Media.DEFAULT_SORT_ORDER);
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
for ( int i = 0 ; i < actualimagecursor.getCount() ; i++ )
{
actualimagecursor.moveToPosition(i);
String fileName = actualimagecursor.getString(actual_image_column_index);
fileList.add(( Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, fileName )));
}
return fileList;
}
catch ( Exception e )
{
return null;
}
}
httpUploader
public class HttpUploader extends AsyncTask<String, Void, String> {
protected String doInBackground(String... path) {
String outPut = null;
for (String sdPath:path) {
Bitmap bitmapOrg = BitmapFactory.decodeFile(sdPath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
//Resize the image
double width = bitmapOrg.getWidth();
double height = bitmapOrg.getHeight();
double ratio = 400/width;
int newheight = (int)(ratio*height);
// System.out.println("———-width" + width);
//System.out.println("———-height" + height);
bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, 400, newheight, true);
//Here you can define .PNG as well
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 95, bao);
byte[] ba = bao.toByteArray();
String ba1 = Base64.encodeToString(ba, 0);
//System.out.println("uploading image now ——–" + ba1);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image", ba1));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://technolsys.net/imageupload.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// print responce
outPut = EntityUtils.toString(entity);
Log.i("GET RESPONSE—-", outPut);
//is = entity.getContent();
Log.e("log_tag ******", "good connection");
bitmapOrg.recycle();
} catch (Exception e) {
Log.e("log_tag ******", "Error in http connection " + e.toString());
}
}
return outPut;
}
}
我想出解決方案。 實際上,我的SD卡中沒有數據(EXTERNAL_CONTENT_URI)
因此這就是列表返回null的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.