i am new to android programming. i am working on a project in which i have to send images from android to php server. code was working fine until today when i run again the project i am getting these errors. i don't know what's went wrong into my code. please help me
These are the errors
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)
here is my code
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;
}
}
i figure out the solution. actually there were no data in my sd card (EXTERNAL_CONTENT_URI)
so thats why the list was returning null .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.